Search code examples
iosobjective-cuinavigationbaruiappearance

Back Button does not color accordingly (objective c)


I’m currently working on an iphone/ipad app that has multiple navigation bar colours. In one part of the app the navigation bar has to be orange and in the other part it has to be purple. On the start screen of the app the navigation bar is hidden.

The colour of the navigation bar, back button and bar button is set in prepareForSegue according to the segue identifier for both sections of the app.

UIImage *navBackgroundImage = [UIImage imageNamed:@"Navigation-Bar-Orange.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

//Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:@"Back-Orange"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

//Change the appearance of standard navigation button
UIImage *barButtonImage = [[UIImage imageNamed:@"Standard-Orange"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], UITextAttributeTextColor,
                                                       [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],UITextAttributeTextShadowColor,
                                                       [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
                                                       UITextAttributeTextShadowOffset,
                                                       [UIFont fontWithName:@"Futura-CondensedMedium" size:21.0], UITextAttributeFont, nil]];

When switching to the other part of the application (going through the segue again), the navigation bar and standard bar button will be coloured correctly, but the back button will have the colour of the previous part of the app. I’ve tried setting the colour on several occasions in the application (e.g. viewWillAppear and viewDidLoad), but the problem still occurs.

How can I fix this problem and show the correct button colour for both sections of the application? Is there maybe another (better) way to set different navigation bar colours?

Visual example of the occuring problem:

https://i.sstatic.net/kJKuT.png


Solution

  • I've set the actual backbarbutton by making a new UIBarButtonItem and adding it to the UINavigationBar.

    UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:nil];
        [barBtnItem setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        self.navigationItem.backBarButtonItem = barBtnItem;
    

    Thank you for guiding me in the right direction Marcus.