【发布时间】:2016-09-22 08:23:43
【问题描述】:
我编写了自己的 Button、Textfield、...、类。在“自定义类”的情节提要中,我将类设置为 UIElement。这很好用。
现在我需要一个以编程方式添加的工具栏。当我在 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 方法中自定义