简体   繁体   中英

UINavigationBar appearance + iOS 13 Dark Mode - How to set app wide color?

In iOS 12 and before I used [[UINavigationBar appearance] setBackgroundColor...] to set a app wide background color for all NavBars.

In iOS 13 I would like to do the same while supporting Dark Mode. In the apps Asset Catalog I defined a named Color NavBarBackground and specified both a Any appearance and Dark appearance color.

If the Dark Mode is disabled the correct Any color is used. However when Dark Mode is enabled the specified Dark color is ignored and all NavBar appear in plain black...

However, if I set the background color of a NavBar manually in IB to NavBarBackground this one NavBar shows the correct color both in Normal and in Dark Mode.

So, how to use [UINavigationBar appearance] together with Dark Mode and named colors?

You can use iOS 13's new appearance API: https://developer.apple.com/documentation/uikit/uinavigationbarappearance

Example:

let style = UINavigationBarAppearance()
style.backgroundColor = .red
style.barTintColor = UIColor(named: "my_colour")!

navigationController?.navigationBar.standardAppearance = style
navigationController?.navigationBar.scrollEdgeAppearance = ...
navigationController?.navigationBar.compactAppearance = ...

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