ActionSheet 呈现不正确的颜色

时间:2021-06-01 03:27:45

标签: ios swift swiftui

我的应用正在使用 Assets.xcassets 中定义的 AccentColor。我将 AccentColor 设置为黄色,但是 ActionSheets 仍然使用默认的蓝色。触摸按钮后按钮的颜色会发生变化,但在关闭并再次打开工作表后会重置为蓝色。

最小复制

struct ContentView: View {
  @State private var show = false
  var body: some View {
    VStack {
      Button(action: { show = true }) {
        Text("Open")
          .actionSheet(isPresented: $show) {
            let btns: [ActionSheet.Button] = [
              .default(Text(LocalizedStringKey("Button 1")), action: {}),
              .destructive(Text(LocalizedStringKey("Button 2")), action: {}),
              .default(Text(LocalizedStringKey("Button 3")), action: {}),
              .cancel()
            ]
            
            return ActionSheet(title: Text("Action Sheet"), buttons: btns)
          }
      }
    }
  }
}

模拟器设备屏幕录制

Screen recording of iOS Simulator

1 个答案:

答案 0 :(得分:1)

它看起来像一个错误(主要是因为某些操作表以正确的颜色呈现,而有些则没有)。我找不到原因,只是正确渲染了一些工作表。

在修复之前,有一个技巧可以解决我的问题。它并不完美,因为它覆盖了 tintColor 的默认 UIAlertController,但它有效。就像在 SceneDelegate 中使用一行代码一样简单:

// SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  // ...
  // Any color may be used instead of UIColor(named: "AccentColor")
  UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(named: "AccentColor")
  // ...
}