Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...