自定义UIBarButtonItem“完成”风格和“平原”风格分别使用UIAppearance [英] Customizing UIBarButtonItem "Done" style and "Plain" style separately using UIAppearance

查看:204
本文介绍了自定义UIBarButtonItem“完成”风格和“平原”风格分别使用UIAppearance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 -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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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