UINavigationBar background image does not change correctly after orientation change on iPhone 5

Long title, hopefully a little problem.

I have a UINavigationBar with a custom background image. My application will support landscape orientation as well as portrait. The landscape works well in the simulator for the retina of 3-inch and non-mesh screens. However, on a 4-inch retina screen, the background image is doubled in size in landscape mode.

Here is the corresponding code snippet from my user init-method navigation controller:

[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar-bg.png"] forBarMetrics:UIBarMetricsDefault]; if (IS_IPHONE_5) { [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar-bg-landscape-iphone5.png"] forBarMetrics:UIBarMetricsLandscapePhone]; } else { [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar-bg-landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone]; } 

IS_IPHONE_5 is a macro defined as:

 #define WIDTH_IPHONE_5 568 #define IS_IPHONE_5 ([[UIScreen mainScreen] bounds].size.height == WIDTH_IPHONE_5) 

Here are 2 screen fragments that could explain things more clearly. When the application opens in portrait mode, everything is in order:

portrait

All are reset when changing in landscape mode:

landscape

Image dimensions (in pixels, width x height) for the landscape version of the background image:

  • navbar-bg-landscape.png: 480x44
  • navbar-bg-landscape@2x.png : 960x88
  • navbar-bg-landscape-iphone5.png: 1136x88

Or can it be a problem only for the simulator? (Now I do not have a real iPhone 5)

+7
source share
1 answer

I believe that your navbar-bg-landscape-iphone5.png should be called navbar-bg-landscape-iphone5@2x.png and that you should continue to refer to it as navbar-bg-landscape-iphone5.png in your code. Since the iPhone 5 has a retina display, iOS will look for the @2x version and use it. If he does not find it, he will use the version you specified, and then increase it by 2x. To avoid increasing the size of the ix iOS, give it a @2x version.

+7
source

All Articles