我正在尝试创建一个自定义UINavigationBar
类,然后使用Storyboard
将其设置为我的班级UINavigationController
的导航栏。
这是我的代码UINavigationBar
class:
class CustomNavBar: UINavigationBar {
override func drawRect(rect: CGRect) {
super.drawRect(rect)
// Drawing code
self.backgroundColor = UIColor.orangeColor()
let myLabel = UILabel(frame: CGRect(x: 0, y: 4, width: 600, height: 36))
myLabel.backgroundColor = UIColor.purpleColor()
myLabel.textColor = UIColor.yellowColor()
myLabel.text = "Custom NavBar!"
self.addSubview(myLabel)
}
}
然后,在 Interface Builder 中,我使用Identity Inspector
将其设置为该类NavigationBar
of my UINavigationController
.
当我运行该应用程序时 - 它冻结了。它挂在LaunchScreen.xib
并且什么也不做。
为什么要这么做?这样做的正确方法是什么?
在斯威夫特上:
文件 - 新建 - 文件 - iOS 源 - Cocoa Touch 类 - 选择 UINavigationBar 的子类并将其命名为 - CustomNavigationBar。
class CustomNavigationBar: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.redColor()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override func drawRect(rect: CGRect) {
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)