How to set background for Navigation Bar in iOS?

iOSiPhone/iPadMobile Development

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
raja
Published on 22-Jan-2019 17:07:25
Advertisements