ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • sheet (Q)
    Swift/SwiftUI 2023. 8. 2. 01:12
    Button("포켓몬 추가") {
    
        self.isPresented.toggle()
                        
        }.sheet(isPresented: $isPresented) {
        
            //띄울 뷰 & 뷰에 전달할 바인딩 값
            AddPocketmonView(pocketmonStore: pocketmonStore, isShowingAddSheet: $isShowingAddSheet)
        }

    sheet(isPresented:onDismiss:content:)

    Presents a sheet / when a binding to a Boolean value that you provide is true.
     
     
    ⚠️ 오류: 바인딩 값 변수를 같게 (isPresented)로 통일한다. 
     

     

    예제 코드 
    struct ContentView: View {
        @State private var showingSheet: Bool = false
        
        var body: some View {
            VStack {
                Button {
                    showingSheet.toggle()
                } label: {
                    Text("Show sheet!")
                        .font(.largeTitle)
                }
            }
            .sheet(isPresented: $showingSheet) {
                Text("This is sheet!")
    //                .presentationDetents([.medium, .large])
    //                .presentationDetents([.height(140)])
                    .presentationDetents([.fraction(0.70)])
    //                .presentationDragIndicator(.hidden)
            }
        }
    }

    1. 버튼을 눌러서 showingSheet가 true가 되면 sheet가 나타난다. 

     

    struct ShowLicenseAgreement: View {
        @State private var isShowingSheet = false
        var body: some View {
            Button(action: {
                isShowingSheet.toggle()
            }) {
                Text("Show License Agreement")
            }
            .sheet(isPresented: $isShowingSheet,
                   onDismiss: didDismiss) {
                VStack {
                    Text("License Agreement")
                        .font(.title)
                        .padding(50)
                    Text("""
                            Terms and conditions go here.
                        """)
                        .padding(50)
                    Button("Dismiss",
                           action: { isShowingSheet.toggle() })
                }
            }
        }
    
    
        func didDismiss() {
            // Handle the dismissing action.
        }
    }

     

     

    버튼에도 붙일 수 있고, Vstack에도 붙일 수 있다. 

     

    
    import SwiftUI
    
    struct ContentView: View {
        @State private var show: Bool = false
        @State private var show2: Bool = false
    
        var body: some View {
            VStack {
                Button {
                    show.toggle()
                            } label: {
                                Text("Show sheet1!")
                                    .font(.largeTitle)
                            }
                Button {
                    show2.toggle()
                            } label: {
                                Text("Show sheet2")
                                    .font(.largeTitle)
                            }
                            .sheet(isPresented: $show2) {
                                        Text("시트2")
                          
                                            .presentationDetents([.fraction(0.70)])
                            
                                    }
               
            }
            .sheet(isPresented: $show) {
                        Text("시트1!")
          
                            .presentationDetents([.fraction(0.70)])
           
                    }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }

    Q) 바인딩한 변수의 값에 따라 시트가 나타난다면 sheet코드의 위치는 상관 없는 것인가?

     

Designed by Tistory.