I want to change the navigation bar background colour, title font and colour programmatically in iOS 11 and swift 4 from AppDelegate. I know how to do it using Xcode but didn't find up-to-date solution for doing it programmatically.
You can use the following code to change background colour and Title font.
func setupNavigationBarAppearance() {
UINavigationBar.appearance().tintColor = .black
UINavigationBar.appearance().shadowImage = UIImage.imageFromColor(.black, width: 1.0, height: 1.0)?.resizableImage(withCapInsets: .zero, resizingMode: .tile)
UINavigationBar.appearance().isTranslucent = false
let font:UIFont = UIFont(name: "ProximaNova-Bold", size: 18.0)!
let navbarTitleAtt = [
NSAttributedStringKey.font:font,
NSAttributedStringKey.foregroundColor: UIColor.white
]
UINavigationBar.appearance().titleTextAttributes = navbarTitleAtt
}
And call this func in didFinishLaunchingWithOptions as setupNavigationBarAppearance(). I am using this same code, and it is working fine.
Here are the steps for doing it for only specific ViewControllers.
I have created a BaseViewController file which is the parent for all of my ViewControllers. And the following code as been added to the viewDidLoad() of the BaseViewController.
For changing the Navigation bar's background color
self.navigationController?.navigationBar.barTintColor = UIColor.white
For changing Navigation bar's title and Bar button colors
self.navigationController?.navigationBar.tintColor = UIColor.black
For changing font
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font : UIFont.sourceSansPro(ofSize: 18.0), NSAttributedStringKey.kern:1.5]
You can use the following code to change background colour and Title font.
func setupNavigationBarAppearance() {
UINavigationBar.appearance().tintColor = .black
UINavigationBar.appearance().shadowImage = UIImage.imageFromColor(.black, width: 1.0, height: 1.0)?.resizableImage(withCapInsets: .zero, resizingMode: .tile)
UINavigationBar.appearance().isTranslucent = false
let font:UIFont = UIFont(name: "ProximaNova-Bold", size: 18.0)!
let navbarTitleAtt = [
NSAttributedStringKey.font:font,
NSAttributedStringKey.foregroundColor: UIColor.white
]
UINavigationBar.appearance().titleTextAttributes = navbarTitleAtt
}
And call this func in didFinishLaunchingWithOptions as setupNavigationBarAppearance(). I am using this same code, and it is working fine.
Just use UINavigationBar.appearance()
For example:
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.white]
or
UINavigationBar.appearance().barTintColor = .blue
For AppDelegate:
Put following code in didFinishLaunchingWithOptions
in AppDelegate
:
UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
–
Covenant On iOS 15 it is now
navigationController.navigationBar.backgroundColor = .white
© 2022 - 2024 — McMap. All rights reserved.