今回は下から出てくる選択肢アラートの実装をしてみようと思います。
普段はAndroidの開発をしています。
AndroidにはそういったUIはないので最初名前が何て言うのか調べました。
ActionSheetと、言うみたいです。
ですが、SwiftUiでは別の名前になってました。
confirmationDialogという名前だったので、こちら使用しています。
SampleAlert.swift
struct SampleAlert: View {
@State private var showConfirmationDialog = false
var body: some View {
VStack {
Button("SampleConfirmationDialog") { showConfirmationDialog = true }
}
.padding()
.confirmationDialog("", isPresented: $showConfirmationDialog) {
Button {
// TODO: 押した時の処理
} label: {
Text("追加1")
}
Button {
// TODO: 押した時の処理
} label: {
Text("追加1")
}
Button("キャンセル", role: .cancel) {}
}
}
}
こんな感じの表示になりました。
SwiftUI慣れている方はすぐにわかると思います!
isPresentedに表示のフラグを設定して、表示したいボタンを記載することになります。
以上です。