更改导航栏图像 - 无法正常工作

时间:2013-07-09 11:23:02

标签: ios image background uinavigationbar

我想更改导航栏的背景图片(或颜色),但由于某种原因它无效。

我一直在搜索,发现在iOS 5.0+(我正在运行6.1)上这应该可行:

[self.navigationController.navigationBar setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];

现在这些都不起作用。还试过:

self.navigationController.navigationBar.backgroundColor = [UIColor redColor];

现在我想我正在将它分配给错误的对象,但这有效:

UIImageView *titleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scr1_title"]];
self.navigationItem.titleView = titleImage;

3 个答案:

答案 0 :(得分:4)

在视图控制器中使用此代码更改导航栏背景图像。

 if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
    {
        UIImage * navBarImage = [UIImage imageNamed:@"image.png"] ;
        [[UINavigationBar appearance]setBackgroundImage: navBarImage  forBarMetrics:UIBarMetricsDefault];
}

答案 1 :(得分:0)

您可以在导航控制器的导航栏上设置色调颜色。确保选择正确的打开,如下面的屏幕截图所示。  Screenshot

  1. 在文件浏览器中选择您的故事板。
  2. 在情节提要中选择导航控制器。
  3. 在文档大纲(第二列)中,选择导航控制器的导航栏。
  4. 在“实用工具”视图(右侧列)中调整色调颜色。
  5. 单击导航控制器右侧的根视图控制器。点击它后它会拾取新颜色。

答案 2 :(得分:0)

设置图片.. 写下这段代码

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar"] forBarMetrics:UIBarMetricsDefault];

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}
相关问题