0

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.

Nike Kov
  • 12,630
  • 8
  • 75
  • 122
  • Your code is working. Didn't you forget to call the `appearance` function? – Mojtaba Hosseini Oct 01 '19 at 14:47
  • @MojtabaHosseini nope. It is working in light color mode as needed. – Nike Kov Oct 01 '19 at 14:53
  • Can't reproduce. The large title switches colors just fine for me. Please show enough code _in context_ to allow the problem to be reproduced - not just the little snippets you _think_ are enough. – matt Oct 01 '19 at 15:18
  • @matt what else you need? Thats only code is rely to problem, placed in `applicationDidFinishLaunching`. – Nike Kov Oct 02 '19 at 08:15
  • What else do _you_ need? Would you like me to post an example project proving that your code works just fine? I mean, of course I don't know what `UIFont.SFProDisplay` means, but everything else I can just copy and use directly and the large title changes color as expected. (I used Georgia as the font instead.) – matt Oct 02 '19 at 12:18
  • Here you go: https://github.com/mattneub/LargeTitleTest Download. Run. Change between dark and light modes. Look. – matt Oct 02 '19 at 12:40
  • @matt thanks for your time. I did add more explanation to the question. – Nike Kov Oct 03 '19 at 13:21

2 Answers2

1

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 = // ...
    }
}
Nike Kov
  • 12,630
  • 8
  • 75
  • 122
matt
  • 515,959
  • 87
  • 875
  • 1,141
0

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 = ...
Ash Cameron
  • 1,898
  • 1
  • 10
  • 11