在UINavigationController中将UIImage居中 [英] Center UIImage in UINavigationController

查看:48
本文介绍了在UINavigationController中将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外观] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];[[UINavigationBar外观] setBarStyle:UIBarStyleBlack];[navController.navigationBar setContentMode:UIViewContentModeCenter];navController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; 

图像在iPhone 6上的所有设备 上均能正常显示.图像以1x,2x,Retina 4 2x,3x和iPad 2x尺寸存储在Images.xcassets中./p>

任何指针或建议将不胜感激.预先非常感谢.

更新:

我已经取得了一些进展.从此提示中获得提示,我进行了一些更改:

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

此功能在所有情况下均适用,但肖像iPhone 6(非加号)除外(该应用程序在iPhone上不旋转.)在右边.

作为记录,逐步浏览iPhone模拟器中运行的代码时使用的图像是640x128,分辨率为144 ppi.

更新2:

似乎iPhone 6正在使用列为2x的图像(在.xcassets文件中).此文件与iPhone 4(在模拟器中)所使用的文件相同.在iPhone 6上正确显示,但在iPhone 4上显示不正确.

解决方案

检查此内容:

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

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.

解决方案

Check this:

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

这篇关于在UINavigationController中将UIImage居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆