Custom UINavigationBar with iOS 5

What’s new for iOS 5?

Well for one, a major upgrade to the UIKit contained in the seemingly simple UIAppearance protocol.

With the UIAppearance protocol, you can modify the appearance of all instances of a particular object. This is very powerful, but what it did was break a lot of old code.

Before the UIAppearance protocol, Developers had to do many strange things in order to get a custom look to their UINavigationBar or UITabBar. Up to, and including overriding the -(void)drawRect: method of the objects themselves.

For example, you could customize the UINavigationBar prior to iOS 5 as follows:

@implementation UINavigationBar (CUSTOM_DRAW)

- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@_%@",@"navbar", ((UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)) ? @"l" : @"p")]];
[img drawInRect:CGRectMake(0, 0, self.fsw, self.fsh)];
}

@end

Ugh… what a lot of code. Just to handle different orientations and devices.

Now, however, the same can be done with just a few lines of code:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_p"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_l"] forBarMetrics:UIBarMetricsLandscapePhone];

And Voila! Now you have a custom appearance UINavigationBar.

BUT… what happens if you don’t upgrade to iOS 5? Anyone running iOS 5 with an old iOS 4.x app is going to be very disappointed. Most of the old methods of getting custom appearances are completely overridden by the new UIAppearance Protocol. Even overriding the -(void)drawRect: method I put above is ignored in favor of the new stuff. So that nice custom-looking Navigation Bar or Tab Bar? Gone. You get bland old iPhone blue… which is going to make your otherwise very pretty app – very ugly.

So, make sure your apps are running under iOS 5 – not just in function, but also in appearance.

Stephen Furlani

Stephen Furlani

Stephen has worked as a Biomedical Software Engineer on real-time and medical imaging applications on both Windows and Mac OS platforms respectively. As a Mobile Developer he leverages that experience on current projects.

Categories

Search

Recent Posts

Most Common Tags