简体   繁体   English

导航栏颜色设置不正确

[英]Navigation bar color not set properly

I'm trying to set my navigation bar color. 我正在尝试设置导航栏颜色。 I don't use storyboards in this app so it must be done in code. 我不在这个应用程序中使用故事板,所以它必须在代码中完成。 Currently I have this: 目前我有这个:

 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] 

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

Why color above navigation bar is not the same? 为什么导航栏上面的颜色不一样?

Try below code with Your RGB Color code 使用您的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)

Just put this code in viewDidLoad() 只需将此代码放在viewDidLoad()

Hope Its Work !! 希望它的工作!

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

More about UIAppearance API in Swift you can read here: https://developer.apple.com/documentation/uikit/uiappearance 有关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

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

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