Mike's solution is great.
I offer an alternative approach to change the UINavigationBar color which applies to any iOS version.
We are basically going to exploit the fact that we can set an Image as the UINavigationBar's background.
FIRST
Add an extension to generate a UIImage from any UColor. Note that you can also also write an extension later to generate UIColor from hexadecimals or other formats if you want.
extension UIColor {
func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
return UIGraphicsImageRenderer(size: size).image { rendererContext in
self.setFill()
rendererContext.fill(CGRect(origin: .zero, size: size))
}
}
}
Thanks @neoneye for this great UIColor
extension :)
SECOND
Now we get down to business:
private func setupNavigationBarAppearance(navBar: UINavigationBar) {
navBar.isTranslucent = false
let navBarColorImage = UIColor.blue.image()
navBar.setBackgroundImage(navBarColorImage, for: .default)
navBar.tintColor = UIColor.white
}
Because we've setup the opaque colored image as the background, we do not need to check for iOS 13.
I hope this help some folks to customize their NavBars.
Cheers!