【问题标题】:Customizing an Alert in SwiftUI在 SwiftUI 中自定义警报
【发布时间】:2020-07-07 06:37:55
【问题描述】:

在使用SwiftUIiOS 应用程序中,我使用这种代码设置了警报:

  Alert(title: Text("Nice Title"),
        message: Text("Some important message for the user."),
        dismissButton: .none)

它有效,但我想更改一个细节。警报有一个蓝色的确定按钮。我想对其进行自定义,使其具有灰色的确定按钮。

我尝试了一些调整,其中之一是:

  Alert(title: Text("Nice Title"),
        message: Text("Some important message for the user."),
        dismissButton: .default(Text("OK").foregroundColor(.gray)))

但我的试验都没有奏效。

如果有人知道要走的路,请告诉我。

【问题讨论】:

  • 这是当前版本的 swiftUI 无法做到的。在 wwdc 和 ios 14 上展示的 beta xcode 是可能的
  • dismissButton 需要一个 Alert.Button。只有少数可用 Text() .. 所以没有机会用背景设置它的样式。唯一的选择可能是创建自己的 Alert.Button。不确定,是否可以通过扩展来实现

标签: ios swiftui customization alert


【解决方案1】:

把它放在显示 z 警报的视图中

    init() {
        UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .purple1
    }

【讨论】: