UINavigationController中的中心UIImage

时间:2014-12-15 18:32:51

标签: ios iphone ipad uiimage

我正在尝试将背景UIImage集中在一个在代码中实例化的UINavigationController中:

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中。< / p>

任何指针或建议都将不胜感激。非常感谢提前。

更新:

我取得了一些进展。从this answer得到一些提示我做了一些改动:

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上旋转。)现在图像缩放得更大,而不是左对齐,现在缩放得更大,太远了在右边。

对于记录,当单步执行iPhone模拟器中运行的代码为640x128(144 ppi)时,正在使用该图像。

更新2:

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

2 个答案:

答案 0 :(得分:0)

我不确定这是否是正确的答案,但我发现它似乎本身并不显示为iPhone 6和6 plus应用程序,除非你的应用程序有更大的iPhone的启动图像。

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

答案 1 :(得分:0)

检查一下:

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