The*_*anu 5 colors ios swift swiftui actionsheet
我有一个关于 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()
])
}
Run Code Online (Sandbox Code Playgroud)
问题是操作的颜色是两个按钮的默认颜色(“蓝色”)。我可以通过在“SceneDelegate.swift”或什至在上面的代码中添加以下行来更改这一点。
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(named: "green")
Run Code Online (Sandbox Code Playgroud)
这条线的问题是它会覆盖从“蓝色”到“绿色”的一般颜色。仍然需要找到有关如何为每个动作着色的解决方案。
这是它的样子: 图像预览
你有什么建议吗?
还有另一种按钮样式用于删除等操作
.actionSheet(isPresented: $showingActionSheet) {
return ActionSheet(title: Text("Delete images"), buttons: [
.destructive(Text("Delete selected")){
// some action to do
},
.cancel()
])
}
Run Code Online (Sandbox Code Playgroud)