How to set background for Navigation Bar in iOS?

To set the background color for a navigation bar we can either do it programmatically or through the storyboard if it is on storyboard.

Method 1

Let's see how to change the background color of a navigation bar through the storyboard editor.

  • Create a new project, select it's view controller and embed in navigation controller.

  • Select the navigation bar and go to It's attribute inspector.

This is how it looks in the Xcode 10. You can select the tint color from there and it will be changed for the navigation controller.

Method 2

Programmatically changing the navigation background.

To programmatically change it, go to the view controller and inside, ViewDidLoad or ViewWillAppear write the following code:

self.navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)

to change other properties like text color, tint color or transparency you can use

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started