iphone - 在 iOS 5 中为 UINavigationBar 设置不同的背景图像

标签 iphone uinavigationbar background-image

我的应用程序有多个 View Controller ,对于其中一些 View Controller ,我想在将它们推送到导航堆栈时使用不同的导航栏背景图像。

例如,当我的 mainViewController 加载时,我有这个:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"Setting toolbar for iOS 5+");
    UIImage * navbarImage = [UIImage imageNamed:@"navbar01.png"];
    [[UINavigationBar appearance] setBackgroundImage:navbarImage forBarMetrics:UIBarMetricsDefault];
}

同样,当我的另一个 viewController 被推送到导航堆栈时,我使用与上面相同的代码,除了使用不同的 UIImage (navbar02.png)。

但是,导航栏与我设置的第一张图像相比没有变化。当新 View 出现时,有什么方法可以更改 UINavigationBar 背景图像吗?

谢谢!

最佳答案

以下是类似问题的链接:UINavigationBar setBackgroundImage: forBarMetrics: Not Working

答案是使用这个:

[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

而不是您使用过的代码。

关于iphone - 在 iOS 5 中为 UINavigationBar 设置不同的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099663/

相关文章:

ios - iPhone 设备中的位置问题

iphone - 获取iPhone/iPad屏幕上显示的文字

ios7 - 禁用导航栏的后退按钮

html - 带滚动条的背景图片

html - 如何将菜单按钮放在菜单栏背景的顶部

css - 使背景图像响应

ios - 如何在 swift 中使用 google 回调 url 方案返回到您的应用程序?

ios - 将 MapKit 区域按钮操作转换为方法

ios - 设置导航栏颜色

ios - 当 leftBarButtonItem 工作正常时,RightBarButtonItem 未显示