这是我的班级有覆盖的内容
class BottomView: UIView {
// MARK: - initilization
override init() {
println("init")
super.init()
setup()
}
override init(frame: CGRect) {
println("init frame")
super.init(frame: frame)
setup()
}
required init(coder aDecoder: NSCoder) {
println("init decoder")
super.init(coder: aDecoder)
setup()
}
func setup() {
println("setup")
}
}
然后我使用以下代码
在我的ViewController中初始化class ViewController: UIViewController {
let bottomView = BottomView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
bottomView.frame = CGRectMake(0.0, self.view.frame.height/2.0, self.view.frame.width, self.view.frame.height/2.0)
bottomView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleTopMargin
self.view.addSubview(bottomView)
}
}
我的输出是
init
init frame
setup
setup
所以我的问题是,为什么要调用init(frame :)?
答案 0 :(得分:3)
super.init()
来电self.init(frame:...)
。从本质上讲,它只是说frame
是可选的;如果你没有通过它,他们会分配一个(0,0,0,0)
的框架。
func init() {
self.init(frame:CGRectMake(0,0,0,0));
}