ActionSheet
Example
struct ContentView: View {
@State private var showActionSheet = false
var body: some View {
Button("Eat 🍌") {
showActionSheet = true
}
.actionSheet(isPresented: $showActionSheet) {
ActionSheet(
title: Text("Food alert!"),
message: Text("You have made a selection"),
buttons: [
.cancel(),
.destructive(Text("Change to 🍑")) { /* override */ },
.default(Text("Confirm")) { /* confirm */ }
]
)
}
}
}
Menu
Example
struct MenuView: View {
var body: some View {
Menu {
Button("Open in Preview", action: { })
Button("Save as PDF", action: { })
} label: {
Image(systemName: "doc")
Text("PDF")
}
}
}
ContextMenu
Example
func selectHearts() {
// Act on hearts selection.
}
func selectClubs() { }
func selectSpades() { }
func selectDiamonds() { }
let menuItems = ContextMenu {
Button("♥️ - Hearts", action: selectHearts)
Button("♣️ - Clubs", action: selectClubs)
Button("♠️ - Spades", action: selectSpades)
Button("♦️ - Diamonds", action: selectDiamonds)
}
struct ContextMenuMenuItems: View {
private var shouldShowMenu = true
var body: some View {
VStack {
Text("Favorite Card Suit")
.padding()
.contextMenu(shouldShowMenu ? menuItems : nil)
}
}
}
参考链接: