UINavigationBar background image – iOS 5.0
Posted on | October 17, 2011 | No Comments
While the earlier post works perfect for iOS versions of 4.3 and below, iOS 5 brings us new way to customize UI Interface elements, without the need to override the - (void)drawRect:(CGRect)rect method of an interface element. Fortunately older binaries with the above implementation still work due link-checking binaries build with iOS version below 5.0.
So how do we achieve the above in iOS 5.0. Behold the UIAppearance protocol.
Following the protocol we can set the background image of a UINavigationBar by accessing the appearance property and supplying a UIImage, like so:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed: @"my_uinavigationbar_image.png"] forBarMetrics:UIBarMetricsDefault];
Besides changing the background image the UIAppearance Protocol has more utility methods to change the way interface elements look. E.g changing the title appearance has just gotten a lot easier:
NSDictionary *textAttributes =
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"my_custom_fontname" size:21], UITextAttributeFont,
[UIColor redColor], UITextAttributeTextColor,
nil];
[[UINavigationBar appearance] setTitleTextAttributes:textAttributes];
No since these methods are only available to devices with iOS 5 installed and if you want to support lower os versions you will have to protect these methods with some runtime check if they are available. e.g. by calling
if([instance respondsToSelector(@selector(iOS5_method))]{ //Safe to use iOS 5 method... }
I hope this post helps fellow programmers with the transition to iOS 5.
Comments
Leave a Reply