我尝试在 contextMenu
中配置按钮,但它不起作用。
Text("A label that have context menu")
.contextMenu {
Button(action: {
// remove it
}) {
Text("Remove")
.foregroundColor(.red) // Not working
Image(systemName: "trash")
}.disabled(true) // Not working
}
我有什么:
我正在寻找什么:(删除和调用按钮)
我会在UIKit
中创建一个UIAction
,如下所示,但我找不到任何修饰符,或者无论如何无法将其带到SwiftUI :
let delete = UIAction(title: "Remove", image: UIImage(systemName: "trash"), attributes: .destructive) { action in
// remove it
}
最佳答案
iOS 15 现在支持所有询问的情况
破坏性:(适用于 iOS 15)
将 .delta
设置为按钮的 role
参数:
Button(role: .destructive) { // 👈 This argument
// delete something
} label: {
Label("Delete", systemImage: "trash")
}
<小时/>
禁用:(适用于 iOS 14.2)
向按钮添加 .disabled
修饰符。
Button {
// call someone
} label: {
Label("Call", systemImage: "phone")
}.disabled(true) // 👈 This modifier
<小时/>
分隔符:(适用于 iOS 14)
直接使用 Divider()
View 。
完整演示:
关于swift - 如何在 SwiftUI 中配置删除和禁用 ContextMenu 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58467846/