0
1

SwiftUIで下から出てくる選択肢アラートの実装方法

Last updated at Posted at 2023-10-01

今回は下から出てくる選択肢アラートの実装をしてみようと思います。

普段は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) {}
        }
    }
}

スクリーンショット 2023-10-01 17.21.36.png

こんな感じの表示になりました。
SwiftUI慣れている方はすぐにわかると思います!
isPresentedに表示のフラグを設定して、表示したいボタンを記載することになります。

以上です。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1