我写了我自己的Button,Textfield,...类。在“自定义类”的故事板中,我将该类设置为UIElement。这工作得很好。需要的init?(编码器aDecoder:NSCoder)不叫
现在我需要一个以编程方式添加的工具栏。当我在我的ViewController中添加工具栏时,一切都很好。但是我想创建我自己的工具栏类。
class MyOwnToolbar : UIToolbar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//never called
self.backgroundColor = UIColor.redColor()
self.tintColor = UIColor.greenColor()
self.barTintColor = UIColor.blueColor()
}
override init(frame: CGRect) {
//error: super.init isn'T called on all paths before returning from initiliazer
}
在我的ViewController我尝试这样调用:
fromToolBar = MyOwnToolBar() //call nothing?
fromToolBar = MyOwnToolBar(frame: CGRectMake(0,0,0,0)) //doesn't work because init(frame: CGRECT) doesnt work
旧的代码在我的ViewController奏效:
self.untilToolBar = UIToolbar(frame: CGRectMake(0,0,0,0))
untilToolBar?.backgroundColor = redColor
untilToolBar?.tintColor = greenColor
untilToolBar?.barTintColor = blueColor
所以,我可以用我的工作的解决方案,但我想解开为什么我的代码无法正常工作。所以也许有人有解决方案或良好的联系。
你需要定制awakeFromNib方法 –
http://stackoverflow.com/a/29783546/2303865内 –