Search code examples
ios7uinavigationcontrolleruinavigationbarcustomization

UINavigationBar customization lost on popViewControllerAnimated


My setup

Working with iOS 7, inside a UINavigationController i'm pushing 3 UIViewControllers, each of them customize the UINavigationBar with different look but in this way:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self customizeNavBar];
}
- (void) customizeNavBar
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;

    CALayer *navBar = [CALayer layer];
    [navBar setBackgroundColor:[[UIColor whiteColor] CGColor]];
    [navBar setFrame:CGRectMake(0, -20, 320, 64)];
    [navBar setOpacity:0.4];

    [self.navigationController.navigationBar.layer insertSublayer:navBar atIndex:0];
}

And there isn't any customization on the AppDelegate.

My problem

Everything works fine when pushing VC's, but when popping back it's the last customization applied the one that remains and i don't found a way to apply the corresponding customization again.


Solution

  • First of all, iOS 7 Navigation Bar colors is headache and change from 7.0 to 7.1 versions. But here is a solution that may be work for you.

    You have to remove the navBar CALayer when you show a new VC so the navbar color updates properly when you pop VC.

    The only problem with this solution is during transition between a transulcent Nav Bar to a no translucent nav bar and viceversa you'll notice the change.

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        self.navigationBar.translucent = NO;
        [self.navColourView.layer removeFromSuperlayer];
    }
    
    - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        self.navigationBar.translucent = YES;
        [self.navigationBar.layer insertSublayer:self.navColourView.layer atIndex:1];
    }
    

    Here is a discussion about this topic https://gist.github.com/alanzeino/6619253