Customize navigation bar appearance with swift
Change navigation bar color
In order to change color of navigation bar for all view controllers, you have to set it in
AppDelegate.swiftfileAdd following code to
didFinishLaunchingWithOptionsfunction inAppDelegate.swift
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517)
In here
tintColorattribute change the background color of the navigation barbarTintColorattribute affect to the color of the
- back indicator image
- button titles
- button images
- This code not affect to the color of navigation bar title. It still remains on black color


Change color of navigation bar title
- Add following code to
didFinishLaunchingWithOptionsfunction inAppDelegate.swift
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517)
// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]
-
titleTextAttributesaffect to the title text, in here I'm setting the title text color white

Programatically change navigation bar title
- In order to change title of the navigation item(in ViewController) you have to set the title in
viewDidLoadfunction of ViewController
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "New title"
}
- Following is an example output after this change

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 barproperty in yourinfo.plistfile(In setting it toNO)

- If you open it via
vim(vim <path to project>/info.plist) you can see it contains a property callUIViewControllerBasedStatusBarAppearancewithfalsevalue

- Then define the color of status bar in
didFinishLaunchingWithOptionsfunction inAppDelegate.swift(This will affect to all view controllers in your app, since it define in AppDelegate.swift)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

Define colors in hex values
- In above codes I have define a custom function to define the colors as
hexvalues
func uicolorFromHex(rgbValue:UInt32)->UIColor{
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
let blue = CGFloat(rgbValue & 0xFF)/256.0
return UIColor(red:red, green:green, blue:blue, alpha:1.0)
}
This function converts hex values to
UIColor(with red, green and blue)More info https://coderwall.com/p/6rfitq
Related protips:
Some simple Swift Extensions (Strip HTML, RGB Color, Color invert, Dismiss modal segue)
Written by eranga bandara
Related protips
15 Responses
how can I set NavigationBar programmatically without StoryBoard?
I added in viewDidLoad()
self.navigationItem.title = "New Title"
but it doesn't work. :(
@xperad: If you just added a UINavigationBar (not a UINavigationController), you'll have to use the following to set the title.
self.navigationBar.topItem.title = "New Title"
In enums in Swift, you can just use the value without specifying the name of the enum Like so,
UIApplication.sharedApplication().statusBarStyle =.LightContent
I'm trying to change navigation bar color but in my AppDelegate.swift, there is no function didFinishLaunchingWithOptions. It's passed in to the application function.
So where do I add the code to customize the navigation bar.
Any idea how to align the title to the left?
Could you provide an example with a blurred background on the navigation bar? :)
you could just do self.title = "New Title" in the viewDidLoad
Is there a way to remove "<" when i go to next screen.
You're rgb division is off its not 256 but 255
@venkatp, to hide the backButton:
navigationItem.setHidesBackButton(true, animated: false)
You used a variable and named it navigationBarAppearace and you meant navigationBarAppearance
barTintColor - is a color of background. Not tint color.
For example, to modify the bar background tint color for all instances of UINavigationBar:
[[UINavigationBar appearance] setBarTintColor:myNavBarBackgroundColor];
https://developer.apple.com/documentation/uikit/uiappearance
Thanks for sharing such amazing shortcuts.
If you're updating the navigation bar appearance from a background thread, you may encounter issues with UI updates not being performed on the main thread. Always ensure that any updates to the UI are done on the main thread using techniques like DispatchQueue.main.async.
Thanks for sharing, I'm just learning this and will find it very useful