NavigationBarStyle issue when switching from iOS 4 to 5
-
26-10-2019 - |
Question
Have a little design issue after having upgraded to iOS 5 and Xcode 4.2
This is how my view looked in iOS 4:
And this is how it looks like in iOS 5:
In my navigation delegate I have the following method to draw the "image" at the top:
- (void)drawRect:(CGRect)rect {
UIImage *image;
if(self.barStyle == UIBarStyleDefault){
image = [UIImage imageNamed: @"topbar_base.png"];
}
else{
image = [UIImage imageNamed: @"nyhedsbar_base.png"];
}
[image drawInRect:CGRectMake(-1, -1, self.frame.size.width+3, self.frame.size.height+3)];
}
And inside my controller I set the following:
self.navigationBarStyle = UIBarStyleBlack;
How come it is not working in iOS 5?
Thanks
Solution
Under iOS5, you need to use UIAppearance
. Have a look at that. Here's an example for using it conditionally so that you can continue to support iOS4:
// iOS5-only to customize the nav bar appearance
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
UIImage *img = [UIImage imageNamed: @"NavBarBackground.png"];
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
As you can see, this sets a custom background image for all UINavigationBars. There are lots of things you can do with UIAppearance. You'll want to keep any custom stuff you're currently doing in drawRect:
since pre-iOS4 devices will still use that and not the new UIAppearance code.