Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back button strangely disappearing in UINavigationController but keeps working

Under iOS7 I've been experiencing an issue where the back button item will not show up if it has been set with a specific background image:

int imageSize = 21; //REPLACE WITH YOUR IMAGE WIDTH

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0)
                                                   forBarMetrics:UIBarMetricsDefault];
UIImage *barBackBtnImg = [[UIImage imageNamed:@"BackArrowDark.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:barBackBtnImg
                                                forState:UIControlStateNormal
                                              barMetrics:UIBarMetricsDefault];

Upon doing this, any ViewController that I push in the navigation controller will have no back button item appearing, even though pressing where it should be, will make it appear, and any subsequent pushes of this view controller will have the button present on the screen.

This issue is only appearing under iOS7: everything works perfectly under iOS6.

Changing the back button completely with a leftBarButtonItem disables the back swipe, so that is not an option.

Any idea what I am doing wrong?

Thanks much for your consideration.

like image 351
Frédéric Lemieux Avatar asked Oct 07 '13 23:10

Frédéric Lemieux


2 Answers

After trying different solutions, I found that changing the backIndicatorImage works best under iOS7, and it seems to be in line with the iOS7 interface paradigm:

[[UINavigationBar appearance] setTintColor:[UIColor grayColor]];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault]; // Takes out title

UIImage *backButtonImage = [UIImage imageNamed:@"BackArrowDark.png"];

if ([UINavigationBar instancesRespondToSelector:@selector(setBackIndicatorImage:)]) {
  [[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
  [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];
} else {
  int imageSize = 21; // REPLACE WITH YOUR IMAGE WIDTH

  [[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}

With this method:

  • When going back in the navigation controller, the back button item transition is the same as with the default indicator (a departure from the back button sliding away as well under iOS6);
  • Under iOS6, the backButton is changed and keeps its default iOS6 behaviour.
  • I'm happy!
like image 122
Frédéric Lemieux Avatar answered Nov 12 '22 11:11

Frédéric Lemieux


Make sure you are not calling this in the view controller:

self.navigationController.navigationBar.tintColor = [UIColor redColor];

In iOS 7, this will tint the navigation bar but will also make your buttons invisible, yet functional just as you are describing.

like image 20
Patrick Lynch Avatar answered Nov 12 '22 11:11

Patrick Lynch