1
votes

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?

1
add and try this in your appdelegate if #available(iOS 13.0, *) { // disable dark mode window?.overrideUserInterfaceStyle = .light } it will change the light to entire appAnbu.Karthik
Thanks but this not what I meant. I want to see different color in Normal and in Dark Mode. Problem is, that in Dark Mode it shows the default color black instead of the specified one.Andrei Herford

1 Answers

1
votes

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