iOS5更改UIToolbar的背景 [英] iOS5 changing Background of UIToolbar

查看:63
本文介绍了iOS5更改UIToolbar的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了"iOS 5的新功能"文档,并指出,现在更好地支持更改某些UI元素的背景.

I've read the "What's new in iOS 5" documentation and there it is stated, that changing the background of some UI-Elements is now better supported.

我找不到iOS 5更改UIToolbar的背景图像的正确方法. iOS 5是否有特殊的新方法?还是我仍然必须继承UIToolbar?

I couldn't find the correct way for iOS 5 to change the background image of an UIToolbar. Is there a special new iOS 5 way of doing this? Or do i still have to subclass the UIToolbar?

推荐答案

是的,有一种新方法可以做到这一点.您可以使用appearance使所有UIToolBar具有相同的外观.

Yes, there is a new way to do this. You can use appearance to make all UIToolBars have the same appearance.

首先,您必须确保您的类遵循UIAppearanceContainer协议.在这里,我已经在我的应用程序委托中做到了:

First, you have to make sure your class follows the UIAppearanceContainer protocol. Here I have done it in my app delegate:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>

@property (strong, nonatomic) UIWindow *window;

@end

然后您可以在application:didFinishLaunchingWithOptions:viewDidLoad中设置外观.像这样:

Then you can set the appearance in, for example, application:didFinishLaunchingWithOptions: or viewDidLoad. Like this:

UIImage *image = [UIImage imageNamed:@"myimage.png"];
[[UIToolbar appearance] setBackgroundImage:image forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

您只需执行一次即可为您应用中的所有UIToolBar获得相同的外观.您还可以设置UIToolBar的许多(如果不是全部?)属性.

You only have to do this once to get the same appearance for all UIToolBars in your app. You can also set many of (if not all?) the properties of your UIToolBar.

作为旁注,有许多类可以遵循UIAppearanceContainer协议.要找出可以使用外观协议进行自定义的内容,可以打开要自定义的类的头文件,如果可以使用UIAppearance设置属性,则该属性的属性声明后面会写有UI_APPEARANCE_SELECTOR.

As a sidenote, there are many classes that can follow the UIAppearanceContainer protocol. To find out what can be customized using the appearance protocol, you can open the header file of the class you want to customize, if you can set a property with UIAppearance, then the property has UI_APPEARANCE_SELECTOR written behind the property declaration.

这篇关于iOS5更改UIToolbar的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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