0
votes

Where should I set my navigation bar's appearance?

I have a view controller called "MainVC" that is nested inside a navigation controller. In "MainVC", I overwrite viewWillAppear and set the background color of my navigation controller's navigation bar to yellow. However, when I launch the app, the color is not set to yellow

When I navigate away from MainVC, to another VC, and then come back to MainVC, the color gets correctly set!!

Why is this happening, and where should I add the code so the navigation bar's background color is appropriately set in MainVC?

Thank you!

2

2 Answers

1
votes

It will work if you place your navigation appearance items in your mainVC's init method, and also in your ViewWillAppear, like this:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self==nil)
        return nil;
    [[[self navigationController] navigationBar] setTranslucent:YES];
    [[[self navigationController ] navigationBar] setBarTintColor:[UIColor clearColor]];
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
     [super viewWillAppear:animated];
     [[[self navigationController] navigationBar] setTranslucent:YES];
     [[[self navigationController ] navigationBar] setBarTintColor:[UIColor clearColor]];
}

No need to set up anything else, just keep it all in your mainVC's implementation file, and it will work, good luck. Oh yes, and on another note, here's why this works:

ViewDidLoad is going to be called a single time in the navigation stack until it's popped off the stack, so you initialize the view controller with the attributes you want for it's navigation controller, you can do this with each viewcontroller to control the navigaiotnbar's view. Putting this in the init method set its up with this before the view is loaded on the screen, and then putting this in your viewWillAppear sets it up so that when you transition from view controller to view controller on the stack, assuming this view controller is still on the stack, the viewWillAppear will change the navigationbar back to the styling you want given. So, the first time that this view controller is loaded onto the stack, the navigaiton bar styling will happen two times, no big deal, but from then on out, it will only happen one time when it is appearing again while on the navigation stack.

0
votes

Set in MainVC, Where you start Navigation in which you have set Navigation

in ViewDidLoad Method

self.navigationController.navigationBar.barTintColor=ColorNav;
self.navigationController.navigationBar.translucent=FALSE;