- Code in ViewController:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let anyobjectype : AnyObject.Type = (NSClassFromString("testReflect.ABC"))!
// testReflectname, ABCUIVIEW
let nsobjectype : NSObject.Type = anyobjectype as! NSObject.Type
let rec: AnyObject = nsobjectype
let currentView: UIView = rec as! UIView
self.view.addSubview(currentView)
}}
-
The code in
uiview
class ABC: UIView {override init (frame: CGRect) {
super.init(frame: frame) // print("you got it override init") self.backgroundColor = UIColor.green
}
required init? (coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/ / Only override draw () if you perform custom drawing.
/ / An empty implementation adversely affects performance during animation.
override func draw (_ rect: CGRect) {print("you got it draw") //self.backgroundColor = UIColor.brown
}
}
-sharp-sharp-sharp uiview("ABC")