问题
i know that this question looks the same as other around here, but none of them say anything about incompatibility between ios4 and ios5.
In my app i want to customize the pattern of the navigation Bar, but my deployment target is ios4, so i am using the code bellow above the implementation of the appDelegate.m to do this:
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor];
UIImage *image = [UIImage imageNamed: @"nav_bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
@end
When i run the app using the 4.3 simulator, it works properly, but when i simulate in ios5, it doesnt work, the nav bar goes back to the default color.. any help?
Thanks.
回答1:
I'd recommend using your existing code when running on iOS4, and using the new iOS5 features to customize the bar under iOS5.
See here: UINavigationBar's drawRect is not called in iOS 5.0
回答2:
Here's a category that will work on both iOS 4 and 5:
@implementation UINavigationBar (CustomBackground)
- (UIImage *)barBackground
{
return [UIImage imageNamed:@"top-navigation-bar.png"];
}
- (void)didMoveToSuperview
{
//iOS5 only
if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[self setBackgroundImage:[self barBackground] forBarMetrics:UIBarMetricsDefault];
}
}
//this doesn't work on iOS5 but is needed for iOS4 and earlier
- (void)drawRect:(CGRect)rect
{
//draw image
[[self barBackground] drawInRect:rect];
}
@end
回答3:
For iOS5 you can use this approach:
if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
[navigationBar setBackgroundImage:[UIImage imageNamed: @"nav_bar.png"]; forBarMetrics: UIBarMetricsDefault];
}
I usually put it into appDelegate.
来源:https://stackoverflow.com/questions/8255437/navigation-bar-customization-in-ios4-doesnt-work-in-ios5