SwiftUI로 모달 표시를 해보자!
동작
 
「모달 표시」버튼을 누르기→모달 표시
"닫기"or 다시 "모달 표시"를 누르기 → 모달 숨기기
를 전환하고 있습니다.
 프로그램
ContentView.swiftimport SwiftUI
struct ContentView: View {
    @State var screenHeight: CGFloat = 0.0
    @State private var showHalfModal = false
    var body: some View {
        GeometryReader(content: { geometry in
            ZStack{
                Button(action: {
                    self.showHalfModal.toggle()
                }, label: {
                    Text("モーダルを表示")
                        .font(.largeTitle)
                })
                HalfModal(closedButton: $showHalfModal,
                          sizeY: screenHeight,
                          objectHeight: 400.0)
            }
            .onAppear(perform: {
                screenHeight = geometry.size.height  // スクリーンサイズ(縦幅)を取得
            })
        })
        .ignoresSafeArea(.all) // 上下のセーフエリアを無効化
    }
}
struct HalfModal: View {
    @Binding var closedButton: Bool
    let sizeY: CGFloat
    let objectHeight: CGFloat
    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 50.0)
                .foregroundColor(.red)
            VStack {
                Spacer()
                Button(action: {
                    closedButton.toggle()
                }, label: {
                    Text("閉じる")
                        .foregroundColor(.white)
                        .font(.largeTitle)
                })
                Spacer()
            }
        }
        .frame(height: objectHeight)
        .offset(y: closedButton ? sizeY-objectHeight :  sizeY)
    }
}
 프로그램 설명
 GeometryReader로 화면 크기 획득
.ignoresSafeArea(.all) 에서 ContentView의 SafeArea를 사용하지 않도록 설정하고 geometry.size.height 에서 전체 화면의 세로 폭을 가져오고 screenHeight 변수에 할당합니다.
 ZStack에서 동시에 표시
ContentView 와 HalfModal 를 ZStack으로 묶어서 한 화면에 두 개의 View 구성 요소를 표시합니다. 그리고, HalfModal측에서 offset() (표시 위치의 이동)를 선언하는 것으로, 화면 하단에 하프 모달이 표시되게 됩니다.
 오프셋으로 표시 위치 변경
Swift는 진위 값으로 처리를 분기 할 수 있습니다.[Bool] ? [trueの処理]:[falseの処理]
같은 처리를 작성할 수 있습니다.
그러므로
ContentView.swift
import SwiftUI
struct ContentView: View {
    @State var screenHeight: CGFloat = 0.0
    @State private var showHalfModal = false
    var body: some View {
        GeometryReader(content: { geometry in
            ZStack{
                Button(action: {
                    self.showHalfModal.toggle()
                }, label: {
                    Text("モーダルを表示")
                        .font(.largeTitle)
                })
                HalfModal(closedButton: $showHalfModal,
                          sizeY: screenHeight,
                          objectHeight: 400.0)
            }
            .onAppear(perform: {
                screenHeight = geometry.size.height  // スクリーンサイズ(縦幅)を取得
            })
        })
        .ignoresSafeArea(.all) // 上下のセーフエリアを無効化
    }
}
struct HalfModal: View {
    @Binding var closedButton: Bool
    let sizeY: CGFloat
    let objectHeight: CGFloat
    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 50.0)
                .foregroundColor(.red)
            VStack {
                Spacer()
                Button(action: {
                    closedButton.toggle()
                }, label: {
                    Text("閉じる")
                        .foregroundColor(.white)
                        .font(.largeTitle)
                })
                Spacer()
            }
        }
        .frame(height: objectHeight)
        .offset(y: closedButton ? sizeY-objectHeight :  sizeY)
    }
}
프로그램 설명
 GeometryReader로 화면 크기 획득
.ignoresSafeArea(.all) 에서 ContentView의 SafeArea를 사용하지 않도록 설정하고 geometry.size.height 에서 전체 화면의 세로 폭을 가져오고 screenHeight 변수에 할당합니다.
 ZStack에서 동시에 표시
ContentView 와 HalfModal 를 ZStack으로 묶어서 한 화면에 두 개의 View 구성 요소를 표시합니다. 그리고, HalfModal측에서 offset() (표시 위치의 이동)를 선언하는 것으로, 화면 하단에 하프 모달이 표시되게 됩니다.
 오프셋으로 표시 위치 변경
Swift는 진위 값으로 처리를 분기 할 수 있습니다.[Bool] ? [trueの処理]:[falseの処理]
같은 처리를 작성할 수 있습니다.
그러므로
[Bool] ? [trueの処理]:[falseの処理]
라고 지정해 주면 액션을 일으키는(=Bool치를 변경한다)과 다른 View가 표시되는 처리를 구현할 수가 있습니다.
소스 코드
GitHub상에서 공개하고 있으므로, 참고 정도에 부디.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(SwiftUI로 모달 표시를 해보자!), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/r0227n/items/e5214ed272323bd2dd1f
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
Reference
이 문제에 관하여(SwiftUI로 모달 표시를 해보자!), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/r0227n/items/e5214ed272323bd2dd1f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)