【问题标题】:SwiftUI ActionSheet different color for each actionSwiftUI ActionSheet 每个动作都有不同的颜色
【发布时间】:2020-11-09 17:19:26
【问题描述】:

我有一个关于 SwiftUI 中的 Actionsheet 的问题。我想创建一个带有 2 个选项的 ActionSheet:删除和取消。 “删除”按钮为红色,“取消”为绿色。

下面是代码示例:

Button(action: {
                    print("Delete button pressed")
                    self.showingActionSheet = true
                }){
                    Text("Go to actions")
                        .foregroundColor(.green)
                        .font(.body)
                        .padding()
                }
                .actionSheet(isPresented: $showingActionSheet) {                   
                    return ActionSheet(title: Text("Delete images"), buttons: [
                        .default(Text("Delete selected").foregroundColor(.red)){
                            // some action to do
                        },
                        .cancel()
                    ])
                }

问题在于操作的颜色是两个按钮的默认颜色(“蓝色”)。我可以通过在“SceneDelegate.swift”甚至上面的代码中添加以下行来更改它。

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(named: "green")

这一行的问题是它会将一般颜色从“蓝色”覆盖为“绿色”。 仍然需要找到如何为每个动作着色不同的解决方案。

它是这样的: image preview

你有什么建议吗?

【问题讨论】:

    标签: ios swift colors swiftui actionsheet


    【解决方案1】:

    还有另一种按钮样式用于删除等操作

    .actionSheet(isPresented: $showingActionSheet) {
        return ActionSheet(title: Text("Delete images"), buttons: [
            .destructive(Text("Delete selected")){
                // some action to do
            },
            .cancel()
        ])
    }
    

    【讨论】:

    • 感谢您的回复。在我提出的案例中,它运作良好。但是,如果我想要除红色以外的其他颜色(比如说紫色),是否可以这样做?
    猜你喜欢
    • 2017-05-19
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多