Pixelcloud.nl

Mobile development

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.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter
  • MySpace
  • NuJIJ

Comments

Leave a Reply





IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)

What is 7 + 10 ?
Please leave these two fields as-is: