繁体   English   中英

导航栏颜色设置不正确

[英]Navigation bar color not set properly

我正在尝试设置导航栏颜色。 我不在这个应用程序中使用故事板,所以它必须在代码中完成。 目前我有这个:

 navigationController.navigationBar.isHidden = false
 navigationController.navigationBar.backgroundColor = UIColor(named: "tint")

 navigationController.navigationBar.shadowImage = UIImage()
 navigationController.navigationBar.isTranslucent = true
 navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
 navigationController.navigationBar.tintColor = .white
 navigationController.navigationBar.titleTextAttributes  =[NSAttributedString.Key.foregroundColor: UIColor.white] 

实际上我得到了: 在此输入图像描述

为什么导航栏上面的颜色不一样?

使用您的RGB颜色代码尝试以下代码

let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let statusBarColor = UIColor(red: 74/255, green: 171/255, blue: 247/255, alpha: 1.0)
statusBarView.backgroundColor = statusBarColor
view.addSubview(statusBarView)

只需将此代码放在viewDidLoad()

希望它的工作!

UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
UIBarButtonItem.appearance().tintColor = UIColor.magentaColor()
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
UITabBar.appearance().backgroundColor = UIColor.yellowColor();

有关Swift中UIAppearance API的更多信息,请访问: https//developer.apple.com/documentation/uikit/uiappearance

要么

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM