How to change background color of UINavigationItem?

Ega Setya Putra picture Ega Setya Putra · May 11, 2015 · Viewed 12.2k times · Source

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?

Answer

Ashok Londhe picture Ashok Londhe · May 11, 2015

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...!!!