Xcode Can';t将导航栏颜色更改为白色
我可以将导航栏设置为黑色或任何其他颜色,但当我尝试白色时,它只会设置为灰色。出于好奇,以下是我使用的代码:Xcode Can';t将导航栏颜色更改为白色,xcode,colors,navigation,Xcode,Colors,Navigation,我可以将导航栏设置为黑色或任何其他颜色,但当我尝试白色时,它只会设置为灰色。出于好奇,以下是我使用的代码: [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]]; 这是怎么回事?setbatintColor是一种色调,意思是它是有色的——这就是为什么你看到的是灰色而不是白色(如果不是白色的话)。如果要用纯色填充导航栏,请尝试使用setBackGroundColor: [[UINavigationBar appea
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
这是怎么回事?
setbatintColor
是一种色调,意思是它是有色的——这就是为什么你看到的是灰色而不是白色(如果不是白色的话)。如果要用纯色填充导航栏,请尝试使用setBackGroundColor
:
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
如果您仍然希望使用alpha值进行一些控制,则可以使用:
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
从iOS 7、导航栏、选项卡栏和工具栏开始
(以下称为“条”)通过以下方式配置为半透明:
违约半透明条将其颜色与之前的灰色混合
将其与系统定义的alpha值相结合以生成最终结果
用于将条形图与其内容合成的背景色
覆盖
↳
setbatintColor
是一种色调,意味着它是有色的——这就是为什么你看到的是灰色而不是白色(如果不是白色的话)。如果要用纯色填充导航栏,请尝试使用setBackGroundColor
:
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
如果您仍然希望使用alpha值进行一些控制,则可以使用:
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
从iOS 7、导航栏、选项卡栏和工具栏开始
(以下称为“条”)通过以下方式配置为半透明:
违约半透明条将其颜色与之前的灰色混合
将其与系统定义的alpha值相结合以生成最终结果
用于将条形图与其内容合成的背景色
覆盖
↳ 找到了解决办法。只需将半透明设置为off即可
[[UINavigationBar appearance] setTranslucent:NO];
当我停下来想一想的时候,它应该是显而易见的。找到了解决办法。只需将半透明设置为off即可
[[UINavigationBar appearance] setTranslucent:NO];
当我停下来思考时,它应该是显而易见的。如果要设置单个条的颜色,请在.m文件中输入以下内容: 创建导航控制器:
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
更改栏的颜色:
myNavigationController.navigationBar.backgroundColor = [UIColor blueColor];
如果要设置单个条的颜色,请在.m文件中输入以下内容: 创建导航控制器:
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
更改栏的颜色:
myNavigationController.navigationBar.backgroundColor = [UIColor blueColor];
我创建了新类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)
我创建了新类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)
对于iOS13,定制导航栏的新方法是使用UINavigationBarAppearance
覆盖函数视图将出现(u动画:Bool){
超级。视图将显示(动画)
让navBarAppearance=UINavigationBarAppearance()
navBarAppearance.backgroundColor=.white
//或navBarAppearance.configureWithTransparentBackground()
navigationController?.navigationBar.standardAppearance=navBarAppearance
}
覆盖函数视图将消失(u动画:Bool){
超级。视图将消失(动画)
让navBarAppearance=UINavigationBarAppearance()
navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.standardAppearance=navBarAppearance
}
注:
- 如果您的视图包含 滚动视图,并在顶部滚动
用于 风景中的iPhone.compactAppearance
用于其他.standardpearance
覆盖函数视图将出现(u动画:Bool){
超级。视图将显示(动画)
让navBarAppearance=UINavigationBarAppearance()
navBarAppearance.backgroundColor=.white
//或navBarAppearance.configureWithTransparentBackground()
navigationController?.navigationBar.standardAppearance=navBarAppearance
}
覆盖函数视图将消失(u动画:Bool){
超级。视图将消失(动画)
让navBarAppearance=UINavigationBarAppearance()
navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.standardAppearance=navBarAppearance
}
注:
- 如果您的视图包含 滚动视图,并在顶部滚动
用于 风景中的iPhone.compactAppearance
用于其他.standardpearance