Updated for Xcode 12.5
SwiftUI gives us the ActionSheet
view for creating action sheets for the user to choose from, and it works similarly to alerts.
As with alerts, you need to define a property that will track whether the action sheet should be visible or not. Then, use the actionSheet()
modifier to monitor that property, and show an action sheet of your choose when the condition becomes true.
For example, here is an example view that triggers an action sheet when a button is tapped:
struct ContentView: View {
@State private var showingSheet = false
var body: some View {
Button("Show Action Sheet") {
showingSheet = true
}
.actionSheet(isPresented: $showingSheet) {
ActionSheet(
title: Text("What do you want to do?"),
message: Text("There's only one choice..."),
buttons: [.default(Text("Dismiss Action Sheet"))]
)
}
}
}
SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.