例によるSwiftUI(SwiftUI by Example)9 Alerts, action sheets, and menus

アラートを表示する方法
How to show an alert

//-----------------------------------
struct ContentView: View {
    @State private var showingAlert = false

    var body: some View {
        Button(action: {
            self.showingAlert = true
        }) {
            Text("Show Alert")
        }
        .alert(isPresented: $showingAlert) {
            Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
        }
    }
}
//-----------------------------------

1つのビューに複数のアラートを表示する方法
How to show multiple alerts in a single view

struct ContentView: View {
    @State private var showingAlert1 = false
    @State private var showingAlert2 = false

    var body: some View {
        VStack {
            Button("Show 1") {
                self.showingAlert1 = true
            }
            .alert(isPresented: $showingAlert1) {
                Alert(title: Text("One"), message: nil, dismissButton: .cancel())
            }

            Button("Show 2") {
                self.showingAlert2 = true
            }
            .alert(isPresented: $showingAlert2) {
                Alert(title: Text("Two"), message: nil, dismissButton: .cancel())
            }
        }
    }
}

How to add actions to alert buttons

//-----------------------------------
struct ContentView: View {
    @State private var showingAlert = false

    var body: some View {
        Button(action: {
            self.showingAlert = true
        }) {
            Text("Show Alert")
        }
        .alert(isPresented:$showingAlert) {
            Alert(title: Text("Are you sure you want to delete this?"), message: Text("There is no undo"), primaryButton: .destructive(Text("Delete")) {
                    print("Deleting...")
            }, secondaryButton: .cancel())
        }
    }
}
//-----------------------------------

アクションシートの表示方法
How to show an action sheet

struct ContentView: View {
    @State private var showingSheet = false

    var body: some View {
        Button(action: {
            self.showingSheet = true
        }) {
            Text("Show Action Sheet")
        }
        .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"))])
        }
    }
}

コンテキストメニューの表示方法
How to show a context menu

//-----------------------------------
struct ContentView: View {
    var body: some View {
        Text("Options")
            .contextMenu {
                Button(action: {
                    // change country setting
                }) {
                    Text("Choose Country")
                    Image(systemName: "globe")
                }

                Button(action: {
                    // enable geolocation
                }) {
                    Text("Detect Location")
                    Image(systemName: "location.circle")
                }
                Button(action: {
                    // enable geolocation
                }) {
                    Text("Detect Location")
                    Image(systemName: "location.circle")
                }
            }
    }
}//-----------------------------------

fullScreenCover()を使用してフルスクリーンのモーダルビューを表示する方法
How to present a full screen modal view using fullScreenCover()

iOS14の新機能

//-----------------------------------
struct FullScreenModalView: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        VStack {
            Text("This is a modal view")
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.red)
        .edgesIgnoringSafeArea(.all)
        .onTapGesture {
            presentationMode.wrappedValue.dismiss()
        }
    }
}
//-----------------------------------
struct ContentView: View {
    @State private var isPresented = false

    var body: some View {
        Button("Present!") {
            self.isPresented.toggle()
        }
        .fullScreenCover(isPresented: $isPresented, content: FullScreenModalView.init)
    }
}
//-----------------------------------

関連サイト リンク
Display Modal View with SwiftUI

appStoreOverlay()を使用して別のアプリを推奨する方法
How to recommend another app using appStoreOverlay()

import SwiftUI
import StoreKit // 追加

//-----------------------------------
struct ContentView: View {
    @State private var showRecommended = false

    var body: some View {
        Button("Show Recommended App") {
            self.showRecommended.toggle()
        }
        .appStoreOverlay(isPresented: $showRecommended) {
            SKOverlay.AppConfiguration(appIdentifier: "1433151795", position: .bottom)
        }
    }
}
//-----------------------------------

ボタンを押したときにメニューを表示する方法
How to show a menu when a button is pressed

iOS14の新機能

ボタンからポップアップメニューを表示するための専用ビューを提供します。(Menu)


//-----------------------------------
struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Hello, World!").padding()
                .navigationTitle("SwiftUI")
                .toolbar {
                    ToolbarItem(placement: .bottomBar) {
                        Menu("Options") {
                            Button("Press Me") {
                                print("Pressed")
                            }
                            Menu("Advanced") {
                                Button("Press Me") {
                                    print("Pressed")
                                }
                                Button("Press Me") {
                                    print("Pressed")
                                }
                            }
                        }
                    }
                }
        }
    }
}
//-----------------------------------