user1072337
user1072337

Reputation: 12945

Custom nav bar background is not restored when popping view

I am having trouble with my navigation toolbar. I set the navigation toolbar background image in my app delegate, and then have a button that, when clicked, changes the navigation tool bar background image and moves to another view.

This works fine, however, when I hit the back button, and go back to the original view, the navigation toolbar background image does not change back.

Here is the relevant code:

In my first view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //set to change the navigation toolbar to this background image (the first) when this view loads
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"QuickGrocery Logo Updated.jpg"] forBarMetrics:UIBarMetricsDefault];
}

button action:

    - (IBAction)wellBalancedMealsClicked:(id)sender {

    QuickGroceryMasterViewController *masterViewController = [[QuickGroceryMasterViewController alloc] initWithNibName:@"QuickGroceryMasterViewController" bundle:nil];

    [masterViewController setRecipeChoice:1];

//changes the navigation toolbar background image
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Keep It Balanced.jpg"] forBarMetrics:UIBarMetricsDefault];
}

Upvotes: 1

Views: 348

Answers (1)

rob mayoff
rob mayoff

Reputation: 386008

There's just one image for the navigation bar. If you change it, and later you want it to change back, you have to change it back yourself.

I'd suggest you give each of your view controllers a navigationBarImage property. Set a delegate for your navigation controller. In the delegate, add this method:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    UIImage *image = [(id)viewController navigationBarImage];
    if (image)
        [navigationController.navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
}

Upvotes: 2

Related Questions