2011-10-18 32 views
2

更新到Xcode 4.2后,我遇到了麻烦。 在我使用下面的代码来制作自定义导航栏之前,但是当我使用iPhone 5.0模拟器时,它失败了,而在iPhone 4.2模拟器中却没有问题。iOS5中的自定义UINavigation栏

我可以知道什么是问题,我该如何解决这个问题?

非常感谢

@implementation UINavigationBar (UINavigationBarCustomDraw) 
- (void) drawRect:(CGRect)rect { 

    [self setTintColor:[UIColor colorWithRed:0.4f 
            green: 0.0f 
            blue:0.4f 
            alpha:1]]; 

    if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:@""]) 
    { 
     [[UIImage imageNamed:@"purple.jpg"] drawInRect:rect]; 
    } 
} 

@end 

回答

2

如果你需要一些图像定制UINavigationBar的,所以你需要把RootViewController的这段代码是导航堆栈(A> B> C的第一个观点,所以你必须把这个在A)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[UIDevice currentDevice] systemVersion]; 

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) { 

     //iOS 5 
     UIImage *toolBarIMG = [UIImage imageNamed: @"purple.jpg"]; 

     if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
     [[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault]; 
     } 

     } else { 

     //iOS 4 
     [self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"purple.jpg"]] autorelease] atIndex:0]; 
     } 
    } 
} 
2

不要像乔哈斯那样做。这是使用内部类的详细信息,可以更改iOS的更新。在iOS 5上使用外观API。

寻找适合iOS 5和iOS 4的东西,Custom UINavigationBar Background