问题描述
我知道如何使用 -setBackgroundImage:forState:barMetrics:
自定义 UIBarButtonItem
,但我想使用 UIBarButtonItemStyleDone
和 UIBarButtonItemStylePlain
的不同图片。
I know how to customize UIBarButtonItem
using -setBackgroundImage: forState: barMetrics:
, but I would like to use different images for UIBarButtonItemStyleDone
and UIBarButtonItemStylePlain
.
是否有使用 UIAppearance
协议实现此目的的方法?或者每次我想要一个完成样式按钮时我是否必须设置图像?
Is there a way to accomplish this using the UIAppearance
protocol? Or do I have to set the image each time I want a "Done" style button?
(我试着搞乱如下代码:
(I tried messing around with code like the following:
[[UIBarButtonItem外观] setBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarButtonItemStyleDone];
但是只需用完成图像设置每个条形按钮。)
But that just sets every bar button with the "Done" image.)
谢谢!
推荐答案
在 iOS 6 中,您可以使用UIBarButtonItem类的新方法:
In iOS 6 you can use the new method of UIBarButtonItem class:
- (void)setBackgroundImage:(UIImage *)backgroundImage
forState:(UIControlState)state
style:(UIBarButtonItemStyle)style
barMetrics:(UIBarMetrics)barMetrics
It sets the background image for the specified state, style, and metrics.More details are available in the Apple docs
所以要更改所有可以使用的UIBarButtonItem的外观类似于:
So to change the appearance of all UIBarButtonItems you can use something like:
UIImage *doneBackgroundImage = [[UIImage imageNamed:@"button_done.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 4)];
[[UIBarButtonItem appearance] setBackgroundImage:doneBackgroundImage
forState:UIControlStateNormal
style:UIBarButtonItemStyleDone
barMetrics:UIBarMetricsDefault];
这篇关于自定义UIBarButtonItem“完成”风格和“平原”风格分别使用UIAppearance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!