首页 文章

如何从AppDelegate更改UINavigationBar背景颜色

提问于
浏览
82

我知道如何通过这样做来改变 UINavigationBar 背景图像

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

并且我知道如何在每个 Views 中将条形设置为不同的颜色.....现在我想要更改背景颜色而不使用图像到 app delegate 的纯色 . 我不想每次从每个视图设置它,我不想写一个 CGRect .

我试过了 [[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:33/255.0 green:34/255.0 blue:36/255.0 alpha:1.0]]; 但是我没有工作,我找不到任何可以在app委托中工作的代码 .

有谁能请我指出正确的方向?

9 回答

  • 2

    您可以在任何视图控制器中使用此代码来设置UINavigation背景颜色

    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:10.0f/255.0f green:30.0f/255.0f blue:200.0f/255.0f alpha:1.0f];
    
  • 193

    颜色代码是这里的问题 . 而不是使用195/255,使用0.7647或195.f / 255.f问题是转换浮动不能正常工作 . 尝试使用精确浮点值 .

  • 17

    您可以使用Xcode 6.3.1轻松完成此操作 . 在文档大纲中选择您的NavigationBar . 选择“属性”检查器 . 取消选中半透明 . 将Bar Tint设置为所需的颜色 . 完成!

  • 12

    Swift

    self.navigationController?.navigationBar.barTintColor = UIColor.red
    self.navigationController?.navigationBar.isTranslucent = false
    
  • 97

    正如其他答案所提到的,你可以使用 setTintColor: ,但你想要一个纯色,并且不可能设置色调颜色AFAIK .

    解决方案是以编程方式创建图像,并通过 UIAppearance 将该图像设置为所有导航栏的背景图像 . 关于图像的大小,我不确定1x1像素图像是否可行或者您是否需要导航栏的确切大小 . 检查this question的第二个答案以了解如何创建图像 .

    作为一个建议,我不喜欢"overload"应用程序代表与这些类型的东西 . 我倾向于创建一个名为 AppearanceConfiguration 的类,只有一个公共方法 configureAppearance ,我设置了我想要的所有UIAppearance,然后我从app delegate调用该方法 .

  • 3

    你可以用 [[UINavigationBar appearance] setTintColor:myColor];

    从iOS 7开始,您需要设置 [[UINavigationBar appearance] setBarTintColor:myColor];[[UINavigationBar appearance] setTranslucent:NO] .

    [[UINavigationBar appearance] setBarTintColor:myColor];
    [[UINavigationBar appearance] setTranslucent:NO];
    
  • -1

    要更改背景颜色而不是色调,以下代码将起作用:

    [self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
    [self.navigationController.navigationBar setTranslucent:NO];
    
  • 6

    要在iOS 7中执行此操作:

    [[UINavigationBar appearance] setBarTintColor:myColor];
    
  • 4

    Swift syntax:

    UINavigationBar.appearance().barTintColor = UIColor.whiteColor() //changes the Bar Tint Color
    

    我只是把它放在AppDelegate didFinishLaunchingWithOptions中,它在整个应用程序中都存在

相关问题