1

I'm trying to center a background UIImage in a UINavigationController that's instantiated in code:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainMenuViewController];
self.window.rootViewController = navController;
UIImage *navBackgroundImage = [UIImage imageNamed:@"fancy_header_logo"];
navBackgroundImage = [navBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
[navController.navigationBar setContentMode:UIViewContentModeCenter];
navController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

The image displays correctly in portrait on all devices except for the iPhone 6. The image is stored in Images.xcassets in 1x, 2x, Retina 4 2x, 3x, and iPad 2x sizes.

Any pointers or suggestions would be greatly appreciated. Many thanks in advance.

UPDATE:

I've made some progress. Taking a hint from this answer I made some changes:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainMenuViewController];
self.window.rootViewController = navController;
UIImage *navBackgroundImage = [[UIImage imageNamed:@"fancy_header_logo"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, -260, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];    
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];

This now works in all cases except iPhone 6 (not plus) in portrait (the app does not rotate on iPhones.) Now the image is scaled larger and instead of being left justified, it is now scaled larger and is too far to the right.

For the record, the image is being used when stepping through the code running in the iPhone simulator is 640x128 at 144 ppi.

UPDATE 2:

It appears that the iPhone 6 is using the image listed as 2x (in the .xcassets file) This is the same file as used by the iPhone 4 (in the simulator.) I have tried massaging the png file so that it displays right on the iPhone 6, but makes it display incorrectly on the iPhone 4.

Community
  • 1
  • 1
cplater
  • 33
  • 9

2 Answers2

0

Im not sure if this is the correct answer or not but I discovered that it doesn't seem to natively show as an iPhone 6 and 6 plus application unless your app has a launch image for the bigger iPhones.

Try just adding a launch image for these devices :)

DairySeeker
  • 326
  • 3
  • 7
  • These are set correctly, and the app uses the native resolutions, the only problem is the alignment of the header image on the iPhone 6. – cplater Dec 16 '14 at 15:36
0

Check this:

UIImage *image = [UIImage imageNamed: @"logo"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
halfer
  • 19,824
  • 17
  • 99
  • 186
Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
  • Unfortunately, this gets me a blank background. I even tried using navController.navigationItem.titleView = imageView with the same results. – cplater Dec 16 '14 at 15:35
  • The image is also not centered in landscape on iPads. – cplater Dec 16 '14 at 15:45