【问题标题】:How to set iOS 13 dark mode large title color?如何设置 iOS 13 深色模式大标题颜色?
【发布时间】:2020-01-30 21:16:00
【问题描述】:

在我的应用中我使用

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_custom。 但是在颜色模式之间切换时,它只使用 Any Appearance 颜色。未使用深色外观。为什么?

补充:

经过一些研究,我认为接下来应该添加到问题中:在我的应用程序中,我使用切换器在模式之间切换。 Storage.isDarkModeOn = newState // saving in user defaults。那么:

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

其中 PapaViewController 是我应用中所有 UIViewController 的父类。因此,如果 overrideUserInterfaceStyle == .dark 和设备颜色模式 == .light 则会出现错误。如果然后我将设备颜色模式更改为.dark,那么大标题看起来像预期的那样。

【问题讨论】:

  • 您的代码正在运行。你不是忘记调用appearance函数了吗?
  • @MojtabaHosseini 不。它根据需要在浅色模式下工作。
  • 无法复制。大标题切换颜色对我来说很好。请在上下文中显示足够的代码,以便重现问题 - 不仅仅是您认为的小sn-ps就足够了。
  • @matt 你还需要什么?那唯一的代码是依赖问题,放在applicationDidFinishLaunching
  • 还需要什么?您希望我发布一个示例项目来证明您的代码可以正常工作吗?我的意思是,我当然不知道UIFont.SFProDisplay 是什么意思,但是我可以直接复制和使用其他所有内容,并且大标题会按预期改变颜色。 (我使用 Georgia 作为字体。)

标签: ios uinavigationbar ios13 ios-darkmode large-title


【解决方案1】:

您是否尝试过使用 iOS 13 的新外观 API?

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

例子:

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

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

【讨论】:

    【解决方案2】:

    您现在显示的代码的问题只是您正在与错误的视图控制器交谈。它不是 your 视图控制器 (self),您需要更改其 override:它是 root 视图控制器,在这种情况下可能是导航控制器.

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

    【讨论】:

    猜你喜欢
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2021-11-29
    • 2020-06-10
    • 2019-11-22
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多