为了添加自定义预览,您可以使用此https://developer.apple.com/documentation/swiftui/view/contextmenu(menuitems:preview:)。预览应该是符合View
的内容。
要将项目拆分成多个组,请在项目之间添加Divider()
。
为了将Delete
项的颜色更改为红色,请像下面的示例中一样将按钮角色更改为.destructive
。
要向一个项目添加子项,请使用如下的Menu
,但我认为这种方法并不被鼓励。
以下是包含上述所有内容的示例。
.contextMenu {
Menu("This is a menu") {
Button {
doSomething()
} label: {
Text("Do something")
}
}
Button {
doSomethingAgain()
} label: {
Text("Something")
}
Divider()
Button(role: .destructive) {
performDelete()
} label: {
Label("Delete", systemImage: "trash")
}
} preview: {
Text("This is the preview") // you can add anything that conforms to View here
}