0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SwiftUI】iOS15以前でアラートにボタンを3つ以上付ける

0
Last updated at Posted at 2022-08-23

はじめに

iOS15以前のSwiftUIではアラートに3つ以上のボタンをつける事ができません。
解決する方法があったので紹介します。

iOS15以降での方法はこちらを参考にしました

方法

Button(action: {
    let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
    let CancelButton = UIAlertAction(title: NSLocalizedString("キャンセル", comment: ""), style: .cancel) { _ in }
    let ActionButton1 = UIAlertAction(title: "システム設定", style: .default) { _ in
        print("システム設定")
    }
    let ActionButton2 = UIAlertAction(title: "ライトモード", style: .default) { _ in
        print("ライトモード")
    }
    let ActionButton3 = UIAlertAction(title: "ダークモード", style: .default) { _ in
        print("ダークモード")
    }
    alert.addAction(CancelButton)
    alert.addAction(ActionButton1)
    alert.addAction(ActionButton2)
    alert.addAction(ActionButton3)
    let windowScene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
    windowScene?.windows.first?.rootViewController?.present(alert, animated: true, completion: nil)
}) {
    Text("外観モードの変更")
}

おわり

SwiftUIできない事が多い。。。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?