반응형
Notice
Recent Posts
Recent Comments
Link
- Today
- Total
04-16 15:40
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- HTTP
- 티스토리챌린지
- tapply
- swiftUI
- ios
- Python
- Request
- substr
- scheduledTimer
- MVC
- 시각화
- SQL
- ReLU
- cocoapods
- Observable
- deeplearning
- r
- 딥러닝
- SWIFT
- barplot
- 연산자
- rxswift
- struct
- Linux
- decode
- Optional
- sigmoid
- rest api
- 오블완
- 명령어
Archives
iOS 개발 기록 블로그
[SwiftUI] @Environment(\.presentationMode) 사용해서 간편하게 View dismiss 하기 본문
iOS/SwiftUI
[SwiftUI] @Environment(\.presentationMode) 사용해서 간편하게 View dismiss 하기
crazydeer 2024. 3. 11. 11:40반응형
https://developer.apple.com/documentation/swiftui/presentationmode
PresentationMode | Apple Developer Documentation
An indication whether a view is currently presented by another view.
developer.apple.com

NavigationBar에 topLeading 위치에 toolbar로 뒤로가기 이미지를 추가한 후
버튼 이벤트를 간단히 추가하는 방법이 있습니다.
import SwiftUI
public struct SampleView: View {
@State var title: String = "안녕하세유, 반가워유"
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
public var body: some View {
VStack {
Text(title)
.font(.system(size: 26, weight: .bold))
.foregroundColor(.black)
.padding()
...
Spacer()
}
.navigationBarTitleDisplayMode(.inline)
.navigationBarBackButtonHidden()
.toolbar { toolbarContent() }
}
@ToolbarContentBuilder
private func toolbarContent() -> some ToolbarContent {
ToolbarItem(placement: .principal) {
Text("Sample")
}
ToolbarItem(placement: .navigationBarLeading) {
backButton
}
ToolbarItem(placement: .navigationBarTrailing) {
actionButtons
}
}
private var backButton: some View {
Image(systemName: "chevron.backward")
.padding(.horizontal, 8)
.onTapGesture {
self.presentationMode.wrappedValue.dismiss()
}
}
private var actionButtons: some View {
HStack(spacing: 0) {
Image(systemName: "dot.radiowaves.left.and.right")
Image(systemName: "heart.fill")
}
}
}
환경 변수(?)를 추가해줍니다.
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
버튼 onTapGesture { }로 아래처럼 추가해줍니다.
self.presentationMode.wrappedValue.dismiss()
그러면 간단하게 Push했던 View를 Pop할 수 있습니다.
쉽죠? 끝~!
반응형
'iOS > SwiftUI' 카테고리의 다른 글
[SwiftUI] @ToolbarContentBuilder로 가독성 개선 (0) | 2024.03.14 |
---|---|
[SwiftUI] 네비게이션바 알 수 없는 여백 없애버리기 (0) | 2024.03.13 |
[SwiftUI] .navigationBarBackButtonHidden() 처리 후 PopSwipeGesture 버그 수정하기 (0) | 2024.03.12 |
SwiftUI Button의 Background가 클릭되지 않는 경우 (0) | 2023.07.31 |