Change UINavigationbar background colour and title font/colour programmatically
Asked Answered
S

5

6

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.

Seringapatam answered 4/5, 2018 at 7:15 Comment(0)
A
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.

Ariella answered 4/5, 2018 at 7:20 Comment(2)
thx. and i used this line for nav bar tint : UINavigationBar.appearance().barTintColor = Colors.accentSeringapatam
This answer will set the appearance for all the navigation bars in your app. If you need to change for just one controller try the answer below by : Kamal Kumar LakshmanCosy
B
7

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.

  1. For changing the Navigation bar's background color

    self.navigationController?.navigationBar.barTintColor = UIColor.white
    
  2. For changing Navigation bar's title and Bar button colors

    self.navigationController?.navigationBar.tintColor = UIColor.black
    
  3. For changing font

    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font : UIFont.sourceSansPro(ofSize: 18.0), NSAttributedStringKey.kern:1.5]
    
Ballard answered 4/5, 2018 at 7:25 Comment(0)
A
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.

Ariella answered 4/5, 2018 at 7:20 Comment(2)
thx. and i used this line for nav bar tint : UINavigationBar.appearance().barTintColor = Colors.accentSeringapatam
This answer will set the appearance for all the navigation bars in your app. If you need to change for just one controller try the answer below by : Kamal Kumar LakshmanCosy
B
2

Just use UINavigationBar.appearance()

For example:

UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.white]

or

UINavigationBar.appearance().barTintColor = .blue

Brittani answered 4/5, 2018 at 7:18 Comment(0)
K
2

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
Keare answered 4/5, 2018 at 7:20 Comment(1)
NSAttributedStringKey has been renamed, so now this line should be: UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]Covenant
R
0

On iOS 15 it is now

navigationController.navigationBar.backgroundColor = .white
Romney answered 2/9, 2021 at 4:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.