【问题标题】:iOS15 Xcode 13 Global Accent Color not workingiOS15 Xcode 13 Global Accent Color 不起作用
【发布时间】:2021-11-09 11:57:20
【问题描述】:

我正在使用最新的 Xcode 13 测试版和 iOS 14 的应用程序,现在我遇到了这个奇怪的问题: 在 iOS 15 更新之前,我的应用程序的全局强调色工作正常,此时颜色现在设置为默认蓝色,之前它是我的自定义颜色。

这里是资产目录:

这是我的项目设置页面,您可以在其中看到强调色是正确的。

这就是应用程序在构建时的样子。当需要真正的深蓝色/紫色时,颜色为默认蓝色。

【问题讨论】:

  • 我们在一个使用 Xcode 12 创建的项目中也看到了这个问题。我测试了使用 Xcode 13 创建一个新项目,并且构建设置符合预期。如果我们找出原因,将更新解决方案。

标签: swiftui ios15 xcode13


【解决方案1】:

我们在应用中使用了UIAppearance API。我们没有设置 tint 颜色,但在应用完成启动后以某种方式调用任何 UIAppearance API 会导致此行为。

enum AppAppearance {
    static func configure() {
        configureCustomBarApperance()
        UITableView.appearance().backgroundColor = UIColor(named: .primaryBackground)
        UITextView.appearance().backgroundColor = nil
        UIScrollView.appearance().keyboardDismissMode = .interactive
    }

    static func configureCustomBarApperance() {
        let barAppearance = UIBarAppearance()
        barAppearance.configureWithTransparentBackground()
        barAppearance.backgroundColor = UIColor(named: .primaryBackground)
        // Toolbars
        let toolbarAppearance = UIToolbarAppearance(barAppearance: barAppearance)
        UIToolbar.appearance().standardAppearance = toolbarAppearance
        UIToolbar.appearance().compactAppearance = toolbarAppearance
        UIToolbar.appearance().scrollEdgeAppearance = toolbarAppearance
        // Navigation Bars
        let navBarAppearance = UINavigationBarAppearance(barAppearance: barAppearance)
        navBarAppearance.titleTextAttributes[.foregroundColor] = UIColor.secondaryLabel
        UINavigationBar.appearance().standardAppearance = navBarAppearance
        UINavigationBar.appearance().compactAppearance = navBarAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
        // Tab Bars
        let tabBarAppearance = UITabBarAppearance()
        tabBarAppearance.configureWithTransparentBackground()
        tabBarAppearance.backgroundColor = UIColor(named: .secondaryBackground)
        UITabBar.appearance().standardAppearance = tabBarAppearance
        UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
    }
}

我们的解决方案是将所有 UIAppearance API 工作转移到 AppDelegate 的初始化程序中,这为我们解决了问题。因此,与其在应用程序启动完成后调用AppAppearance.configure()...我们从AppDelegate.init 调用它,我们的全球强调色现在得到了认可。

别问我为什么……我不能告诉你。

【讨论】:

    【解决方案2】:

    我终于在thisAppleDeveloperForum 线程上找到了一个临时解决方法

    感谢:@chad_sykes 这个答案

    我找到了另一种解决方案,即在 WindowGroup 的主视图上设置 .accentColor,并在整个应用程序中使用它。

    @main
    struct CurvApp: App {
        var body: some Scene {
            WindowGroup {
                myMainView
                    .accentColor(CurvGlobalAppearance.curvAccentColor)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2021-11-16
      • 2021-03-17
      • 2021-11-17
      • 1970-01-01
      • 2022-01-22
      • 2022-06-20
      相关资源
      最近更新 更多