Customizing the navigation bar in iOS with Swift

Slim Ben Nasrallah
Mar 20, 2017 · 3 min read
Image for post
Image for post

Change navigation bar color

  • In order to change color of navigation bar for all view controllers, you have to set it in AppDelegate.swift file
  • Add following code to didFinishLaunchingWithOptions function in AppDelegate.swift
  • In here tintColor attribute change the background color of the navigation bar
  • barTintColor attribute affect to the color of the
  1. back indicator image
  2. button titles
  3. button images
  • This code not affect to the color of navigation bar title. It still remains on black color
Image for post
Image for post
Image for post
Image for post

Change color of navigation bar title

  • Add following code to didFinishLaunchingWithOptions function in AppDelegate.swift
  • titleTextAttributes affect to the title text, in here I'm setting the title text color white
Image for post
Image for post

Programatically change navigation bar title

  • In order to change title of the navigation item(in ViewController) you have to set the title in viewDidLoad function of ViewController
  • Following is an example output after this change
Image for post
Image for post

Change status bar color

  • In above examples status bar color is Black
  • In order to change the color of status bar we have do two changes
  • First define the property of view controller-based status bar property in your info.plist file(In setting it to NO)
Image for post
Image for post
  • If you open it via vim (vim <path to project>/info.plist) you can see it contains a property call UIViewControllerBasedStatusBarAppearance with false value
Image for post
Image for post
  • Then define the color of status bar in didFinishLaunchingWithOptions function in AppDelegate.swift (This will affect to all view controllers in your app, since it define in AppDelegate.swift)
Image for post
Image for post

Define colors in hex values

  • In above codes I have define a custom function to define the colors as hex values
  • This function converts hex values to UIColor(with red, green and blue)

Devagnos

Devagnos is specialized in web based solutions and mobile…

Slim Ben Nasrallah

Written by

CEO & Founder @devagnos

Devagnos

Devagnos

Devagnos is specialized in web based solutions and mobile applications, we provide top of the edge solutions with the latest worldwide renowned technologies.

Slim Ben Nasrallah

Written by

CEO & Founder @devagnos

Devagnos

Devagnos

Devagnos is specialized in web based solutions and mobile applications, we provide top of the edge solutions with the latest worldwide renowned technologies.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store