从iOS 4切换到5时出现NavigationBarStyle问题

Cas*_*nge 1 iphone coding-style navigationbar ios

升级到iOS 5和Xcode 4.2后出现一些设计问题

这就是我在iOS 4中查看的视图:

1 http://casperslynge.dk/1

这就是它在iOS 5中的样子:

2 http://casperslynge.dk/2

在我的导航委托中,我有以下方法在顶部绘制"图像":

- (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)];
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器内部,我设置了以下内容:

self.navigationBarStyle = UIBarStyleBlack;
Run Code Online (Sandbox Code Playgroud)

怎么会在iOS 5中不起作用?

谢谢

Mar*_*off 6

在iOS5下,您需要使用UIAppearance.看看那个.以下是有条件地使用它的示例,以便您可以继续支持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];
}
Run Code Online (Sandbox Code Playgroud)

如您所见,这为所有 UINavigationBars 设置了自定义背景图像.你可以用UIAppearance做很多事情.您需要保留当前正在进行的任何自定义操作,drawRect:因为iOS4之前的设备仍将使用该设备,而不是新的UIAppearance代码.