简体   繁体   中英

How to set iOS 13 dark mode large title color?

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")!
}

颜色集

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.

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

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

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