如何在 macOS swiftUI 中显示下拉菜单
How to display dropdown menu in macOS swiftUI
我有一个按钮,如果按下,将在下拉菜单中显示项目。但我不确定如何在按下按钮后显示显示项目的菜单。这是一个 macOS 应用程序。
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
Button {
}
label: {
Image(systemName: "bookmark.circle")
.resizable()
.frame(width:24.0, height: 24.0)
}
.buttonStyle(PlainButtonStyle())
//List items that i want to display in the dropdown menu
ForEach((1...5), id: \.self) {
Text("\([=10=])")
Divider()
}
}
}
您可以使用 Menu
:
Menu {
ForEach((1...5), id: \.self) {
Text("\([=10=])")
Divider()
}
} label: {
Image(systemName: "bookmark.circle")
.resizable()
.frame(width:24.0, height: 24.0)
}
如果您想要 right-click 菜单,也可以使用 ContextMenu
我有一个按钮,如果按下,将在下拉菜单中显示项目。但我不确定如何在按下按钮后显示显示项目的菜单。这是一个 macOS 应用程序。
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
Button {
}
label: {
Image(systemName: "bookmark.circle")
.resizable()
.frame(width:24.0, height: 24.0)
}
.buttonStyle(PlainButtonStyle())
//List items that i want to display in the dropdown menu
ForEach((1...5), id: \.self) {
Text("\([=10=])")
Divider()
}
}
}
您可以使用 Menu
:
Menu {
ForEach((1...5), id: \.self) {
Text("\([=10=])")
Divider()
}
} label: {
Image(systemName: "bookmark.circle")
.resizable()
.frame(width:24.0, height: 24.0)
}
如果您想要 right-click 菜单,也可以使用 ContextMenu