我有UINavigationItem
,但在属性检查器中找不到tittle,prompt和back按钮旁边的任何内容
我想知道如何使用代码更改UINavigationItem
背景颜色?还是以编程方式?
答案 0 :(得分:13)
您可以通过代码更改它......
对于Objective-C:
self.navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
self.navigationController?.navigationBar.barTintColor = UIColor.redColor();
在viewDidLoad方法中写上面一行。
对于Swift:
self.navigationController!.navigationBar .setBackgroundImage(UIImage .new(), forBarMetrics: UIBarMetrics.Default)
self.navigationController!.navigationBar.shadowImage = UIImage .new();
self.navigationController!.navigationBar.translucent = true;
self.navigationController!.navigationBar.backgroundColor = UIColor.redColor();
OR
navigationController.navigationBar.titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
您可以根据自己的选择更改颜色。
更改文字栏
{{1}}
查看链接.... Here
看到上面的图片......你喜欢这个屏幕的输出...... !!!
答案 1 :(得分:1)
你不应该使用UINavigationBar的background属性,而应该像这样使用barTintColor:
self.navigationController.navigationBar.barTintColor = UIColor.redColor()
如official documentation中所写,要更改条形背景,您必须访问barTintColor属性:
The tint color to apply to the navigation bar background.
如果要编辑导航栏的样式(如按钮颜色),则应访问barTint属性。如果要编辑navigationItem的样式(如后退按钮),则应编辑按钮属性,而不是UINavigationItem。
答案 2 :(得分:1)
一种可能的解决方案是嵌入视图控制器,它包含导航控制器中的导航项,并访问导航栏的属性颜色:
// Color title 'navigationItem'
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
// Background 'navigationBar'
UINavigationBar.appearance().barTintColor = UIColor.blackColor()
// Color title 'navigationBar'
let color = UIColor.orangeColor()
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes(
[NSForegroundColorAttributeName: color], forState: .Normal)
答案 3 :(得分:0)
对于Swift 4
self.navigationController?.navigationBar.isTranslucent = false;
self.navigationController?.navigationBar.backgroundColor = .white