我正在为我的应用程序使用NavigationBar。在iOS7中,我的导航leftButtonItem图标显示正常(http://prntscr.com/2vaha7)。但在iOS6上,它看起来像是:http://prntscr.com/2vafer。
我的代码:
_item2 = [[UINavigationItem alloc] init];
_item2.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sliderMenu.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];
[self.navigationBar pushNavigationItem:_item2 animated:NO];
如何解决这个视觉问题,而不是在iOS6中也可以正常工作?
最诚挚的问候,
奥德。
答案 0 :(得分:2)
看看:http://www.raywenderlich.com/21703/user-interface-customization-in-ios-6
和:http://iosdevblog.com/2013/01/26/the-recommended-size-for-custom-uibarbuttonitem/
您需要为按钮创建自定义透明背面图像,并将其设置为ios 6的条形按钮的背景图像:
[[UIBarButtonItem appearance] setBackgroundImage:clearImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:clearImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
//编辑:我首先认为这是一个后退按钮,你需要像以下一样调整大小:
UIImage *buttonBack30 = [clearImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
我使用以下代码创建了clearImage,但您只需创建任意颜色的34x60图像:
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGRect)rect {
// Create a 1 by 1 pixel context
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect); // Fill it with your color
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UIImage *clearImage = [self imageWithColor:[UIColor clearColor] andSize:CGRectMake(0, 0, 34, 60)];
[[UIBarButtonItem appearance] setBackgroundImage:backgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:backgroundImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];