简体   繁体   English

UINavigationBar 自定义背景图片

[英]UINavigationBar custom background image

I used these suggested solutions to give my UINavigationBar a custom image.我使用这些建议的解决方案为我的 UINavigationBar 提供了一个自定义图像。 (To make it more explicit: I added a category to UINavigationBar in my AppDelegate.m file). (为了更明确:我在 AppDelegate.m 文件中向 UINavigationBar 添加了一个类别)。 This worked fine so far and I didn't have any problems.到目前为止,这工作得很好,我没有任何问题。 Now however, I was running my app on the recent iOS5 beta.然而,现在我在最近的 iOS5 beta 上运行我的应用程序。 The UINavigationBar is blank now again. UINavigationBar 现在又是空白的。

Since all the other apps I have installed, that use a custom image, still behave the same there must be something "wrong" in my code I guess, that iOS5 now doesn't support anymore.由于我安装的所有其他使用自定义图像的应用程序的行为仍然相同,我猜我的代码中一定存在“错误”,iOS5 现在不再支持。

So does anybody have an idea what could be the problem with my adoption of the above mentioned solutions?那么有人知道我采用上述解决方案可能会出现什么问题吗?

The only way I found to make it work was to create a real subclass of UINavigationBar and then in all the views tell IB to use that custom class.我发现让它工作的唯一方法是创建一个真正的 UINavigationBar 子类,然后在所有视图中告诉 IB 使用该自定义 class。 Not as elegant, though...虽然没有那么优雅...

To support both versions, you need to ask if there is setBackgroundImage:forBarMetrics: method.要支持这两个版本,您需要询问是否有 setBackgroundImage:forBarMetrics: 方法。

Take a look at my blogpost about it: http://www.mladjanantic.com/setting-custom-background-for-uinavigationbar-what-will-work-on-ios5-and-ios4-too/看看我的博文: http://www.mladjanantic.com/setting-custom-background-for-uinavigationbar-what-will-work-on-ios5-and-ios4-too/

Use this code使用此代码

        UIImage *backgroundImage = [UIImage imageNamed:@"strip.png"];
        [upnavbar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

this wil work.这会奏效。

One more possible "hacky" solution is to create a custom view and insert it to UINavigationBar as a subView - may be it will still work:另一种可能的“hacky”解决方案是创建一个自定义视图并将其作为子视图插入 UINavigationBar - 可能它仍然可以工作:

UIView *backgroundView = ...
[navigationBar insertSubview:backgroundView atIndex:0];

Or check updated UINavigationBar class reference in iOS5 docs for built-in method to set custom background或检查更新的 UINavigationBar class 参考 iOS5 文档中的内置方法来设置自定义背景

This worked for me on my toolBar这对我的工具栏有用

//toolBar background image set based on iOS version
    [[UIDevice currentDevice] systemVersion];

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

        //iOS 5
        UIImage *toolBarIMG = [UIImage imageNamed: @"toolBar_brown.png"];  

        if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) { 
            [toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0]; 
        }

    } else {

        //iOS 4
        [toolBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolBar_brown.png"]] autorelease] atIndex:0]; 

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM