Search code examples
iosxcodeuinavigationcontrolleruiappearance

UIBarButtonItem shifting downwards after a UINavigationController push


My app runs in a UINavigationController. When I push in a new view controller from the right, the top right UIBarButtonItem (Edit) shifts down by 1 pixel — the Edit button stays in place, but the graphic noticeable shifts down.

I've noticed this only happens after I set up a UIAppearance proxy for the bar button item. This is my code for doing that:

UIImage *barButton = [[UIImage imageNamed:@"navBarBarButton"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[[UIBarButtonItem appearance] setBackgroundImage:barButton
                                        forState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];

navBarBarButton.png is a 52x31 bar button image.

Other potentially relevant info:

  • This only happens in the iOS 6 simulator — it's fine in iOS 5.
  • This app uses storyboards, and the pushes are handled by segues.
  • The one-pixel displacement only happens once, i.e. it doesn't get pushed down a second time when I go another level into a navigation controller.
  • The Edit button's text stays where it is; only the button graphic moves.
  • I tried starting the app in a different view controller (i.e. the second in the navigation stack). The bar button item shows up at the normal (higher) position, then shifts downwards when I push in a new view. This seems to suggest it's not related to any specific view controller.

Has anyone seen this, know why this is happening, or how to prevent the button from shifting? Thanks.


Solution

  • In case anyone gets here with the same issue, I managed to fix the issue by having my barButton image at the standard 30px height (instead of 31, as above).