I'm trying to change the background color / bar tint color of my nav bars programatically.
I have tried several examples, but some required writing code in every view controller which doesn't make the most sense to me, as I would like to have it subclassed to reduce repeated code.
I created a subclass of UINavigationBar (as I also wanted to change the height).
I have successfully changed the height and it works in my application. However, when changing the color, it doesn't appear to work.
class Navbars: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
//self.backgroundColor = Logic.UIColorFromRGB(rgbValue: 0xff0864, alp: 1.0)
//self.barTintColor = Logic.UIColorFromRGB(rgbValue: 0xff0864, alp: 1.0)
self.backgroundColor = UIColor.blue
self.barTintColor = UIColor.blue
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override func draw(_ rect: CGRect) {
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 205)
}
}
The height changes, so I presumed having the code to change it's color in the init function would work, but it doesn't, and I'm not sure why.
I even then tried to subclass UINavigationController and still nothing happens.
class NavControllers: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.barTintColor = UIColor.brown
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
If anyone knows where I am going wrong, that would be much appreciated. Thank you!
Try out this code
////////////////// set navigation bar tint color ///////////////
self.navigationController.navigationBar.barTintColor = (your color here);
Worked for me :) .