升级到 iOS 5 和 Xcode 4.2 后有一点设计问题
这就是我在 iOS 4 中的 View :
1 http://casperslynge.dk/1
这就是它在 iOS 5 中的样子:
2 http://casperslynge.dk/2
在我的导航委托(delegate)中,我有以下方法在顶部绘制“图像”:
- (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)];
}
在我的 Controller 内部,我设置了以下内容:
self.navigationBarStyle = UIBarStyleBlack;
为什么它在 iOS 5 中不起作用?
谢谢
最佳答案
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];
}
如您所见,这为所有 UINavigationBars 设置了自定义背景图像。你可以用 UIAppearance 做很多事情。您需要将当前正在执行的任何自定义内容保留在
drawRect:
中。因为 iOS4 之前的设备仍将使用它,而不是新的 UIAppearance 代码。
关于iphone - 从 iOS 4 切换到 5 时出现 NavigationBarStyle 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7811300/