sheet 수정자는 sheetView 에 presentationMode 를 적용시켜 sheet 뷰에서 ‘X’ 버튼을 누르면 닫힐 수 있도록 한다.
Sheet 정의
기본 코드
// MARK : Body
struct ContentView: View {
@State var showView: Bool = false
var body: some View {
ZStack(alignment: .top) {
Color.yellow
.ignoresSafeArea()
VStack {
Button(action: {
showView.toggle()
}) {
Text("Some Sheet")
.font(.title)
.foregroundColor(.black)
}
}
// METHOD 1 - SHEET
.sheet(isPresented: $showView, content: {
sheetView()
})
}
}
}
// SheetView
struct sheetView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
ZStack(alignment: .topLeading) {
Color.blue
.ignoresSafeArea()
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "xmark")
.font(.title)
.foregroundColor(.white)
.padding(20)
}
}
}
}
실행 화면
FullScreenCover
화면 전체를 덮는 뷰를 생성한다.
기본 코드
struct ContentView: View {
@State var showView: Bool = false
var body: some View {
ZStack(alignment: .top) {
Color.yellow
.ignoreSafeArea()
VStack {
Button(action: {
showView.toggle()
}) {
Text("Some Sheet")
.font(.title)
.foregroundColor(.black)
}
}
.fullScreenCover(isPresented: $showView, content: {
sheetView()
})
}
}
}
개발자 Document
https://developer.apple.com/documentation/swiftui/view/sheet(ispresented:ondismiss:content:)
Apple Developer Documentation
developer.apple.com
Apple Developer Documentation
developer.apple.com
'Language > Swift' 카테고리의 다른 글
SwiftUI 에서 Typealias으로 타입 별칭 정하기 (0) | 2023.01.04 |
---|---|
SwiftUI에서 Markup과 Documentation을 추가하는 방법 (0) | 2023.01.03 |
SwiftUI에서 VideoPlayer 이용하기 (0) | 2022.12.31 |
SwiftUI 에서 trim 이용하기 (1) | 2022.12.28 |
SwiftUI에서 콘텐츠 모자이크 처리하기 (Redacted) (0) | 2022.12.27 |
댓글