无法将导航栏颜色更改为白色

时间:2015-07-27 04:23:24

标签: xcode colors navigation

我可以将导航栏设置为黑色或任何其他颜色,但是当我尝试白色时,它只会使其变为灰色。对于好奇,这是我正在使用的代码:

[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];

这里发生了什么?

5 个答案:

答案 0 :(得分:3)

找到解决方案。只是设置半透明就行了。

[[UINavigationBar appearance] setTranslucent:NO];

当我停下来思考它时,它应该是显而易见的。

答案 1 :(得分:2)

如果要设置单个栏的颜色,请将以下内容放在.m文件中:

创建导航控制器:

UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController];

更改栏的颜色:

myNavigationController.navigationBar.backgroundColor = [UIColor blueColor];

答案 2 :(得分:0)

setBarTintColor是一种色调,意味着它有色 - 这就是为什么你看到灰色而不是白色(如果没有覆盖白色)。如果要使用纯色填充导航栏,请尝试使用setBackGroundColor

[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];

如果您仍然想要使用alpha值进行某些控制,可以使用:

[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
  

从iOS 7开始,导航栏,标签栏和工具栏   (以下称为“条形”)被配置为半透明的   默认。半透明条纹将barTintColor与灰色混合   将它与系统定义的alpha值相结合以产生最终结果   用于将条形图与内容合成的背景颜色   覆盖。

UIColor Class Reference

答案 3 :(得分:0)

我创建了新类CustomViewController.swift,如下所示:

import UIKit

class CustomViewController: UINavigationController {

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        let nav = self.navigationBar
        nav.barStyle = UIBarStyle.black
        nav.barTintColor = UIColor.white
        //nav.tintColor = UIColor.white
        nav.backgroundColor = UIColor.white
        nav.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    }
}

除非我在另一个覆盖我的更改的类行中删除,否则它不起作用:

//nc.navigationBar.shadowImage = UIImage()
//nc.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "bar_background"), for: .default)

答案 4 :(得分:0)

对于iOS13,自定义导航栏的新方法是使用UINavigationBarAppearance。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.backgroundColor = .white
    // or navBarAppearance.configureWithTransparentBackground()
    navigationController?.navigationBar.standardAppearance = navBarAppearance
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.configureWithDefaultBackground()
    navigationController?.navigationBar.standardAppearance = navBarAppearance
}

注意:

    如果您的视图包含一个
  • .scrollEdgeAppearance 滚动视图,并在顶部滚动
  • .compactAppearance的 iPhone横屏
  • .standardAppearance
相关问题