导航栏颜色设置不正确

时间:2019-05-20 10:41:41

标签: ios swift uinavigationcontroller uinavigationbar

我正在尝试设置导航栏颜色。我不在此应用中使用情节提要,因此必须通过代码完成。目前,我有这个:

 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] 

实际上,我得到了: enter image description here

导航栏上方的颜色为什么不相同?

2 个答案:

答案 0 :(得分:0)

尝试使用您的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()

希望它能完成工作!

答案 1 :(得分:0)

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

OR

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