Aim: my UINavigationBar has to be customized with a certain background and a specific label font.
This is easily achievable in iOS5 with the appearance protocol. Ok.
What if my user runs < iOS5? He will see default UINavigationBar, but not if I extend UINavigationBar with a category that overrides drawRect.
Question 1: How can I specify in my Prefix.pch a conditional #import of UINavigationBar category extension according to iOS version?
Question 2: Assuming I manage to solve question 1, will this work? I mean, if the device is not iOS5 I am NOT setting [UINavigationBar] appearance] but only importing the category, do I need to do something more?
EDIT: This is the implementation of Vladimir suggestion, I don’t have a iOS4 device nor simulator to test it right now but I think it might work.
@implementation UINavigationBar (iOS4Appearance)
- (void)drawRect:(CGRect)rect
{
if (self.isiOS5) {
[super drawRect];
} else {
UIImage *background = [UIImage imageNamed: @"navigation-bar"];
[background drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
}
- (BOOL)isiOS5
{
return [[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0;
}
@end
Thanks for any help.
I don’t think you can do this with #import, since that’s compile-time, and you can only upload one binary to the app store (which should be iOS-version-independent). Instead, you can test this at runtime. For example, you can override drawRect, but depending on the iOS version you’ll either call super or run your own code.
EDIT:
My suggestion won’t work with plain categories because they replace the original implementation. There are at least two alternatives: