从 iOS 4 切换到 5 时的 NavigationBarStyle 问题

发布于 2024-12-10 08:37:28 字数 820 浏览 0 评论 0原文

升级到 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)];
}

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

self.navigationBarStyle = UIBarStyleBlack;

为什么它在 iOS 5 中不起作用?

谢谢

Have a little design issue after having upgraded to iOS 5 and Xcode 4.2

This is how my view looked in iOS 4:

1 http://casperslynge.dk/1

And this is how it looks like in iOS 5:

2 http://casperslynge.dk/2

In my navigation delegate I have the following method to draw the "image" at the top:

- (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)];
}

And inside my controller I set the following:

self.navigationBarStyle = UIBarStyleBlack;

How come it is not working in iOS 5?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-12-17 08:37:28

在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];
}

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

Under iOS5, you need to use UIAppearance. Have a look at that. Here's an example for using it conditionally so that you can continue to support 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];
}

As you can see, this sets a custom background image for all UINavigationBars. There are lots of things you can do with UIAppearance. You'll want to keep any custom stuff you're currently doing in drawRect: since pre-iOS4 devices will still use that and not the new UIAppearance code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文