5

我对 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")

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

这是它的样子: 图像预览

你有什么建议吗?

4

1 回答 1

3

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

演示

.actionSheet(isPresented: $showingActionSheet) {
    return ActionSheet(title: Text("Delete images"), buttons: [
        .destructive(Text("Delete selected")){
            // some action to do
        },
        .cancel()
    ])
}
于 2020-07-20T13:20:29.200 回答