简体   繁体   中英

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

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()

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

OR

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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