1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| struct MenuView: View { var body: some View { Menu("Options") { Button("Order Now") { print("Order Now") } Button("Adjust Order") { print("Adjust Order") } Button("Cancel") { print("Cancel") } } } }
|
嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| struct MenuView: View { var body: some View { Menu("Options") { Button("Order Now") { print("Order Now") } Menu("Advanced") { Button("Rename") { print("Rename") } Button("Delay") { print("Delay") } } Button("Adjust Order") { print("Adjust Order") } Button("Cancel") { print("Cancel") } } Menu { Button("Order Now") { print("Order Now") } Button("Adjust Order") { print("Adjust Order") } } label: { Label("Options", systemImage: "paperplane") } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| struct MenuView: View { var body: some View { Text("Options") .contextMenu { Button { print("Change country setting") } label: { Label("Choose Country", systemImage: "globe") } Button(action: { print("Enable geolocation") }, label: { Label("Detect Location", systemImage: "location.circle") }) } } }
|
SwiftUI 的 Picker 视图有一个名为 .menu
的专用样式,它显示了一个弹出式菜单的选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| struct MenuView: View { @State private var selection = "Red" let colors = ["Red", "Green", "Blue", "Black", "Tartan"] var body: some View { VStack { Picker("Select a paint color", selection: $selection) { ForEach(colors, id: \.self) { Text($0) } } .pickerStyle(.menu) Text("Selected color: \(selection)") } } }
|