AccentColor 显示错误,颜色被忽略

Jan*_*els 6 assets tint colors swift swiftui

我将名为 AccentColor 的非蓝色添加到我的 iOS app\xe2\x80\x99s 资源目录中。运行我的应用程序时,色调颜色默认为蓝色。

\n

构建设置中的 \xe2\x80\x9cGlobal Accent Color Name\xe2\x80\x9d 已正确设置为 \xe2\x80\x9cAccentColor\xe2\x80\x9d。我还需要设置什么吗?什么设置可以覆盖这个?

\n

Jor*_*n H 2

我在我的应用程序中遇到了同样的问题。它曾经可以工作,但在某些时候所有控件都变成默认的蓝色而不是我的自定义颜色。我确认目标的构建设置中的全局强调颜色名称是正确的,并且资产目录已添加到该目标。

经过一番调试,终于找到了原因。这行代码破坏了全局强调色(使用 Xcode 14 beta 5 测试):

UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(descriptor: UIFontDescriptor.preferredFontDescriptor(withTextStyle: .largeTitle).withDesign(.rounded)!.withSymbolicTraits(.traitBold)!, size: 34)]
Run Code Online (Sandbox Code Playgroud)

然而,这样的事情并不会破坏它:

UINavigationBar.appearance().largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 34)]
Run Code Online (Sandbox Code Playgroud)

超级奇怪。如果您有任何UINavigationBar外观覆盖,请尝试将其注释掉,看看这是否会导致您的问题。

tintColor作为一种解决方法,我正在做的是在 SceneDelegate 中设置窗口(为我的 SwiftUI 应用程序手动添加):

final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        (scene as? UIWindowScene)?.keyWindow?.tintColor = UIColor(named: "AccentColor")
    }
    
}
Run Code Online (Sandbox Code Playgroud)