简体   繁体   中英

different navigation bar background color between iOS 10 & iOS 11

I'm setting my navigation bar color to #479F46 , in iOS 10 it shows as #5DA15D but in iOS 11 it is showing as #4D9D4E

  • what am I doing wrong?
  • and how to solve this?

Code I use in app delegate

func setupNavigationController() {
    UINavigationBar.appearance().barTintColor = UIColor.colorWithHexString(hexStr: "479F46")
    UINavigationBar.appearance().tintColor = UIColor.white  
    let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
    UINavigationBar.appearance().titleTextAttributes = textAttributes
}

EDIT 1

I was using isTranslucent = false for my navigation controller object,

nvc?.navigationBar.isTranslucent = false

but that seemed to not suffice, I added UINavigationBar.appearance().isTranslucent = false as suggested by llb

and now both view OS versions show the color as #4D9D4E

I need them both to show up as #479F46

在此处输入图片说明

It is because of translucency. Set it to false. Also, as @matt suggested, do not use barTintColor . Set backgroundColor instead.

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = <Your color>

Do not attempt to use the barTintColor if you want complete control over a navigation bar's color. The tint color is a tint . You want to dictate the actual color .

There is only one way to do that. No barTintColor , no translucency. Then give the navigation bar a background image consisting of a rectangle of the desired color.

Example:

let sz = CGSize(20,20)
let c = // some UIColor
let r = UIGraphicsImageRenderer(size:sz)
self.navbar.setBackgroundImage( r.image { ctx in
    c.setFill()
    ctx.fill(CGRect(origin:.zero, size:sz))
}, for:.any, barMetrics: .default)

It's related to the color profile, make sure you are using the correct one

xcode 颜色配置文件

check this article for more details:

https://lembergsolutions.com/blog/how-get-right-color-ios-detailed-instruction

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