16

I have a UINavigationItem, but I can't found anything beside tittle, prompt, and back button in attribute inspector

enter image description here

I wonder how can I change my UINavigationItem background color using code? or programmatically?

Ega Setya Putra
  • 1,645
  • 6
  • 23
  • 51

4 Answers4

24

You can change it through code...

For Objective-C:

    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

Write Above line in viewDidLoad method.

For Swift:

    self.navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent

    self.navigationController?.navigationBar.barTintColor  = UIColor.redColor();

OR

    self.navigationController!.navigationBar .setBackgroundImage(UIImage .new(), forBarMetrics: UIBarMetrics.Default)
    self.navigationController!.navigationBar.shadowImage = UIImage .new();
    self.navigationController!.navigationBar.translucent = true;
    self.navigationController!.navigationBar.backgroundColor = UIColor.redColor();

You can change color on your own choice.

To change the bar Text...

navigationController.navigationBar.titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]

See the Link.... Here

enter image description here

See the above image... you like output like this screen right...!!!

Ashok Londhe
  • 1,491
  • 11
  • 29
2

You shouldn't use the background property of UINavigationBar, but instead you should use barTintColor like this:

self.navigationController.navigationBar.barTintColor = UIColor.redColor()

As written in the official documentation to change the bar background you have to access to the barTintColor property:

The tint color to apply to the navigation bar background.

If you want to edit the style of the navigation bar such as button color you should access to the barTint property. If you want to edit the style of an navigationItem such as back button you should edit the button property, not the UINavigationItem's.

Nicolò Ciraci
  • 678
  • 5
  • 23
  • You should learn to read the official documentation: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationBar_Class/index.html#//apple_ref/occ/instp/UINavigationBar/barTintColor "The tint color to apply to the navigation bar background." – Nicolò Ciraci May 11 '15 at 13:20
1

One possible solution is to embed the View Controller it is containing the Navigation Item in a Navigation Controller and access to properties color of Navigation Bar:

// Color title 'navigationItem'
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

// Background 'navigationBar'
UINavigationBar.appearance().barTintColor = UIColor.blackColor()

// Color title 'navigationBar'
let color = UIColor.orangeColor()
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes(
    [NSForegroundColorAttributeName: color], forState: .Normal)
Igor Jerosimić
  • 13,621
  • 6
  • 44
  • 53
1

For Swift 4

  self.navigationController?.navigationBar.isTranslucent = false;
  self.navigationController?.navigationBar.backgroundColor = .white
Ortensia C.
  • 4,666
  • 11
  • 43
  • 70