iOS5 changing Background of UIToolbar

非 Y 不嫁゛ 提交于 2019-12-04 15:30:01

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

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

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];

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.

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.

Here's a great tutorial: http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

Basically, it looks like this:

UIImage *gradientImage32 = [[UIImage imageNamed:@"surf_gradient_textured_32"] 
    resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[[UINavigationBar appearance] setBackgroundImage:gradientImage32 
    forBarMetrics:UIBarMetricsDefault];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!