user3002217
user3002217

Reputation: 23

How to change the background color of NavigationBar in iOS 7

I'm new to Objective-C, today I tried to change color of my Navigation Bar and this works with this code:

appDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0xf4f4f4)];

    return YES;
}

Now I try change specific ViewController navigation bar and this do not work.

ViewController2.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x363636)];
    self.navigationController.navigationBar.translucent = NO;
}

When I enter this ViewContoller his Navigation color is f4f4f4 and when I go other ViewController and come back it changes the color to 363636.

Why this do not work in first time? Can somebody explain this to me..

(Sorry About my English, and thank you.)

Upvotes: 1

Views: 16629

Answers (5)

Vladimir Prigarin
Vladimir Prigarin

Reputation: 474

For iOS 10 you can use barStyle like this:

For white bar color:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

And for black bar color:

self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

BarStyle need set in ViewDidLoad for normal animation work :)

Upvotes: 0

you can try it, I change in my AppDelegate the navigation bar color, so all the app has the same color, but i have to change the navigation bar color in a single view controller so I did it:

in the AppDelegate

let color = UIColor(red:0.24, green:0.72, blue:0.28, alpha:1.0)
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().barTintColor = color

and in the view controller in the viewDidLoad I have this:

self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()

in the viewWillDissappear this:

super.viewWillDisappear(animated)

    let color = UIColor(red:0.24, green:0.72, blue:0.28, alpha:1.0)
    self.navigationController?.navigationBar.barTintColor = color

It returns the navigation bar color to the same color

Upvotes: 0

Nate
Nate

Reputation: 403

when you use

[UINavigationBar appearance] 

it changes all of the navbars. in your viewcontroller, just do

[self.navigationBar setBarTintColor:UIColorFromRGB(0x363636)];

Upvotes: 16

Neeku
Neeku

Reputation: 3653

You can implement what Nathanael said in the -(void) viewDidLoad method of the view controller. But if you're using the storyboard, you can also select the navigation item and change the tint color from the File inspector without having to code.

enter image description here

Upvotes: 3

nick
nick

Reputation: 114

Use tintcolor property of navigation bar to change the color

Upvotes: 0

Related Questions