在UINavigationController中居中UIImage

我想中心一个UINavigationController的背景UIImage在实例化在代码中:

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; 

除了 iPhone 6 之外 ,所有设备上的图像都能正确显示图像。图像以1x,2x,Retina 4 2x,3x和iPad 2x尺寸存储在Images.xcassets中。

任何指针或build议将不胜感激。 提前谢谢了。

更新:

我已经取得了一些进展。 从这个答案提示我做了一些改变:

 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]; 

这现在适用于所有情况除iPhone 6(不加)肖像(应用程序不会在iPhone上旋转)。现在图像缩放较大,而不是左alignment,它现在缩放较大, 。

为了logging,当在iPhone模拟器中运行的代码是144xpi 640×128时,正在使用该图像。

更新2:

看起来,iPhone 6使用列为2x的图像(在.xcassets文件中)这是iPhone 4(在模拟器中)使用的相同的文件。我已经尝试按摩png文件,以便它显示正确iPhone 6,但在iPhone 4上显示不正确。

林不知道这是否是正确的答案,但我发现,它似乎并没有原生显示为一个iPhone 6和6加应用程序,除非你的应用程序有一个大型iPhone的启动图像。

尝试只为这些设备添加启动图像:)

检查这个:

 UIImage *image = [UIImage imageNamed: @"logo"]; UIImageView *imageView = [[UIImageView alloc] initWithImage: image]; self.navigationItem.titleView = imageView;