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.
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?
Yes, there is a new way to do this. You can use appearance
to make all UIToolBar
s 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 UIToolBar
s 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];