Swift4 UIView 自定义
import UIKit
class ViewA: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
print("-------->A")
self.backgroundColor = UIColor.green;
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("-------->B")
self.backgroundColor = UIColor.blue
}
init(string : String) {
super.init(frame: CGRect(x:0,y:0,width:0,height:0))
print("-------->C")
self.backgroundColor = UIColor.orange;
}
}
使用:
var aaa = ViewA();
aaa.frame = CGRect(x:50,y:60,width:100,height:60);
self.view.addSubview(aaa);
执行了:
-------->A
var aaa = ViewA.init(frame: CGRect(x:0,y:0,width:300,height:80));
self.view.addSubview(aaa);
执行了:
-------->A
var aaa = ViewA.init(string: "123456");
aaa.frame = CGRect(x:50,y:60,width:100,height:60);
self.view.addSubview(aaa);
执行了:
-------->C
swift 无法初始化 init()..........