I have a subclass of the UINavigationBar
. How can I set barTintColor
in this subclass?
class NavBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width:UIScreen.main.bounds.width, height: 66)
}
}
You just need to override the initialisers of UINavigationBar
.
class NavBar: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
// Change barTintColor to whatever you would like
self.barTintColor = .red
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width:UIScreen.main.bounds.width, height: 66)
}
}
I've managed to make it work by subclassing UINavigationController
and setting barTintColor
from there:
class NavBarVC: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.barTintColor = UIColor.someColor
}
}
EDIT : In the end I used solution from the @dirtydanee's answer. Thus I don't need to subclass UINavigationController
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.