Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UINavigationbar background colour and title font/colour programmatically

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.

like image 499
mdehghani Avatar asked May 04 '18 07:05

mdehghani


People also ask

How do I change the navigation title text color?

Changing the text color The text color of the navigation bar can be changed using two inbuilt classes: navbar-light: This class will set the color of the text to dark. This is used when using a light background color. navbar-dark: This class will set the color of the text to light.

How do I change the navigation item title color in Swift?

The title color of Navigation Bar can be changed in Storyboard. Go to Attributes inspector of Navigation Controller > Navigation Bar and set the desired color in Title Color menu. Save this answer.


1 Answers

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]
    
like image 69
Kamal Kumar Lakshmanan Avatar answered Sep 30 '22 05:09

Kamal Kumar Lakshmanan