iOS 5和iOS 6之间的UIBarButtonItem外观差异

时间:2013-07-11 08:21:25

标签: ios uiimage uibarbuttonitem uiappearance

我正在使用外观代理在导航栏中设置后退按钮的背景图像。

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[[UIImage imageNamed:@"arrow-back-button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0., 9., 0., 0.)]
                                                  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

这适用于iOS 6,看起来绝对完美。但是,在iOS 5中,背景图像开始重复,几乎就像按钮更高一些。

iOS 5:

iOS 5 version

iOS 6:

iOS 6 version

使用的图像(带有彩色背景以突出显示尺寸)。

Back button image

任何人都可以帮我在iOS 5中使后退按钮看起来正确吗?

编辑:

如果我不使用resizableImageWithCapInsets:,则结果如下。

iOS 5 with no resizableImage

3 个答案:

答案 0 :(得分:1)

如果您将图像与Retina显示器一起使用,请使用@2x"arrow-back-button@2x"对图像进行后缀。

尝试创建UIImage类别并添加此实例方法:

 (UIImage*)resizableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:            (NSInteger)topCapHeight </b>{
UIImage *image = nil;    
float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (osVersion < 6.0) {
    image = [self stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
} else {
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(topCapHeight, leftCapWidth, topCapHeight, leftCapWidth);
    image = [self resizableImageWithCapInsets:edgeInsets];
}
return image;
 }

我怀疑在iOS6中修复了iOS 5错误

答案 1 :(得分:1)

您遇到的平铺行为对于iOS 5.x来说是正常的。这是它知道如何调整图像大小的唯一行为。我不知道为什么它似乎在5.x中调整而不是在6.x中调整,但在iOS 5.x中修复此行为的唯一方法是制作与您的{的背景大小相同的PNG {1}}(使用Alpha填充程序)并将其设置为不可调整大小的背景图像。

答案 2 :(得分:1)

您需要将背景图像更改为30px高(视网膜版本为60px)。只需在顶部和底部添加一些透明像素,使箭头垂直居中。

iOS 5强制最小高度为30pt,如果图像较小,则调整图像大小。