Andrei Herford
Andrei Herford

Reputation: 18729

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?

Upvotes: 1

Views: 4070

Answers (1)

Ash Cameron
Ash Cameron

Reputation: 1948

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 = ...

Upvotes: 1

Related Questions