无法将导航栏颜色更改为白色
Can't change navigation bar color to white
我可以将导航栏设为黑色或任何其他颜色,但当我尝试白色时,它只会变成灰色。出于好奇,这是我正在使用的代码:
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
这是怎么回事?
setBarTintColor
是一种色调,意味着它是有色的——这就是为什么您看到的是灰色而不是白色(如果没有覆盖白色)。如果您想用纯色填充导航栏,请尝试使用 setBackGroundColor
:
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
如果您仍想通过 alpha 值进行一些控制,您可以使用:
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
Beginning with iOS 7, navigation bars, tab bars, and tool bars
(henceforth referred to as 'bars') are configured to be translucent by
default. A translucent bar mixes its barTintColor with gray before
combining it with a system-defined alpha value to produce the final
background color that is used to composite the bar with the content it
overlies.
找到解决方案。只需关闭半透明就可以了。
[[UINavigationBar appearance] setTranslucent:NO];
停下来想一想,应该是显而易见的。
如果您想设置单个条形的颜色,请将以下内容放入您的 .m 文件中:
正在创建导航控制器:
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
更改条形的颜色:
myNavigationController.navigationBar.backgroundColor = [UIColor blueColor];
我做了新的classCustomViewController.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]
}
}
在我删除覆盖我的更改的另一 class 行之前,它不起作用:
//nc.navigationBar.shadowImage = UIImage()
//nc.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "bar_background"), for: .default)
对于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
如果您的视图包含
scrollview 并且在顶部滚动
.compactAppearance
为
iPhone 横向
.standardAppearance
其余
我可以将导航栏设为黑色或任何其他颜色,但当我尝试白色时,它只会变成灰色。出于好奇,这是我正在使用的代码:
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
这是怎么回事?
setBarTintColor
是一种色调,意味着它是有色的——这就是为什么您看到的是灰色而不是白色(如果没有覆盖白色)。如果您想用纯色填充导航栏,请尝试使用 setBackGroundColor
:
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
如果您仍想通过 alpha 值进行一些控制,您可以使用:
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
Beginning with iOS 7, navigation bars, tab bars, and tool bars (henceforth referred to as 'bars') are configured to be translucent by default. A translucent bar mixes its barTintColor with gray before combining it with a system-defined alpha value to produce the final background color that is used to composite the bar with the content it overlies.
找到解决方案。只需关闭半透明就可以了。
[[UINavigationBar appearance] setTranslucent:NO];
停下来想一想,应该是显而易见的。
如果您想设置单个条形的颜色,请将以下内容放入您的 .m 文件中:
正在创建导航控制器:
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
更改条形的颜色:
myNavigationController.navigationBar.backgroundColor = [UIColor blueColor];
我做了新的classCustomViewController.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]
}
}
在我删除覆盖我的更改的另一 class 行之前,它不起作用:
//nc.navigationBar.shadowImage = UIImage()
//nc.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "bar_background"), for: .default)
对于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
如果您的视图包含 scrollview 并且在顶部滚动.compactAppearance
为 iPhone 横向.standardAppearance
其余