How to set iOS 13 dark mode large title color?

1k views Asked by At

In my app i use

let navigationBar = UINavigationBar.appearance()
navigationBar.largeTitleTextAttributes = [
    NSAttributedString.Key.font: UIFont.SFProDisplay(ofSize: 34, weight: .bold),
    NSAttributedString.Key.foregroundColor: UIColor.custom
]

static var custom: UIColor {
    return UIColor(named: "color_custom")!
}

color set

where have a color set color_custom. But when switching between color modes it used only Any Appearance color. Dark Appearance not used. Why?

ADDITION:

After some research i figured that should add to the question next: In my app i use a switcher to switch between modes. Storage.isDarkModeOn = newState // saving in user defaults. Then:

class PapaViewController: UIViewController {
    if #available(iOS 13.0, *) {
        overrideUserInterfaceStyle = Storage.isDarkModeOn ? .dark : .light
    }
}

Where PapaViewController is a parent class for all UIViewControllers in my app. So if overrideUserInterfaceStyle == .dark and device color mode == .light the bug shows up. If then i change the device color mode to .dark then large title looks as expected.

2

There are 2 answers

1
matt On BEST ANSWER

The problem with the code you've shown now is merely that you're talking to the wrong view controller. It is not your view controller (self) whose override you need to change: it is the root view controller, which in this case is probably the navigation controller.

class PapaViewController: UIViewController {
    if #available(iOS 13.0, *) {
        self.navigationController?.overrideUserInterfaceStyle = // ...
    }
}
0
Ash Cameron On

Have you tried using iOS 13's new appearance API?

https://developer.apple.com/documentation/uikit/uinavigationbarappearance

Example:

let style = UINavigationBarAppearance()
style.largeTitleTextAttributes = [.font: #YOURFONT#]

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