SwiftUIでシートを出し分ける


SwiftUIでシートを出し分ける方法です。

SwiftUIでシートを出し分ける

ContentView.swift

import SwiftUI

struct ContentView: View {
    @State var showingSnorlaxView = false
    @State var showingSlowpokeView = false
    
    var body: some View {
        VStack {
            Button(action: {
                showingSnorlaxView = true
            }) {
                Text("Show Snorlax")
            }
            
            Button(action: {
                showingSlowpokeView = true
            }) {
                Text("Show Slowpoke")
            }
        }
        .sheet(isPresented: $showingSnorlaxView) {
            SnorlaxView()
        }
        .sheet(isPresented: $showingSlowpokeView) {
            SlowpokeView()
        }
    }
}

#Preview {
    ContentView()
}

SlowpokeView.swift

import SwiftUI

struct SlowpokeView: View {
    var body: some View {
        Image(.slowpoke)
            .resizable()
            .frame(width: 150, height: 150)
            .scaledToFit()
    }
}

SnorlaxView.swift

import SwiftUI

struct SnorlaxView: View {
    var body: some View {
        Image(.snorlax)
            .resizable()
            .frame(width: 150, height: 150)
            .scaledToFit()
    }
}