
SwiftUI는 네비게이션 바 디스플레이 모드를 설정할 수 있다. 모드를 설정하기 위해서 .navigationBarTitleDisplayMode 수정자를 사용한다. 모드는 3가지가 있다. - inline - large - automatic # inline 네비게이션 항목의 표준 범위 내에 제목을 표시한다. # large 확장된 네비게이션 항목 내에 큰 제목을 표시한다. # automatic 이전 탐색 네비게이션 아이템에서 디스플레이 모드를 상속한다. struct ContentView: View { var body: some View { NavigationStack { Text("HI") .navigationTitle("Title") .navigationBarTitleDisplayMode(.inline) ..
SwiftUI를 사용하다보면 네비게이션 바를 사용하지 않을 때가 있다. 하지만 네비게이션 바를 사용하지 않아도 공간을 차지고 있다. # iOS 13.0 ~ 17.0 네비게이션 바 공간을 없애기 위해서 .navigationBarHidden(true) 수정자를 사용하면 된다. struct TestView: View { var body: some View { NavigationStack { Text("HI") .navigationBarHidden(true) } } } # iOS 16.0 ~ .navigationBarHidden(true)는 iOS 13.0-17.0까지만 지원한다고 한다. 그래서 iOS16.0 부터는 .toolbar(.hidden, for: .navigationBar) 수정자를 사용해야 한다. ..

SwiftUI의 Form의 배경색상을 얻는 방법은 다음 사이트에서 잘 설명해주고 있다. https://sarunw.com/posts/dark-color-cheat-sheet/ Dark color cheat sheet | Sarunw A cheat sheet that tells you what colors to use to support dark mode. This is a guide for those who want to adopt dark mode, but too lazy to figure out which color to use. sarunw.com 나는 Form의 배경색상을 얻는 방법을 원했다. 일반 배경은 .systemBackground라는 하나의 색상이 사용된다. 하지만 Form은 그룹 색상이 ..
SwiftUI에서 코드를 짜다가 정말 예상치도 못한 일이 발생했다. 자식 뷰에서 State를 초기화할 때, 데이터를 확인하기 위해서 print()를 포함시켰다. 그리고 부모 뷰가 리빌드될 때마다, 자식 뷰의 State가 초기화가 되면서 print()가 출력되는 것이였다. 대략적인 코드는 다음과 같다. import SwiftUI struct ContentView: View { @State private var text: String = "" var body: some View { NavigationStack { TextField(text: $text, label: { Text("TextField") }) NavigationLink(destination: { ChildView1() }, label: { Te..
SwiftUI에서 내비게이션을 통해 이전 화면에서 현재화면으로 이동했을 경우, 다음과 같은 방법으로 이전 화면으로 돌아갈 수 있다. 1. 먼저 dismiss 환경을 가져온다. @Environment(\.dismiss) private var dismiss 2. dismiss() 함수를 호출해서 이전 화면으로 돌아간다. dismiss() 다음 SecondView는 Button를 탭하면 이전 화면으로 돌아가는 예제코드이다. struct SecondView: View { @Environment(\.dismiss) private var dismiss var body: some View { Button(action: { dismiss() } }) { Text("Back") } } }

문제의 코드 EditButton은 editMode를 지원하는 컨테이너의 environment의 editMode를 토글한다. 나는 EditButton을 사용 시 EditMode를 감지하여 Text로 출력하고 싶었다. struct ContentView: View { @Environment(\.editMode) private var editMode var body: some View { VStack { NavigationStack { List { Text(editMode?.wrappedValue == .active ? "Edit Mode" : "Normal Mode") ForEach(0..
다음은Label에 동일하게 적용되는 공통 스타일을 사용자 정의 LabelStyle을 만들고, LabelStyle에 사용자 정의 스타일을 확장하고 사용하는 예제이다. import SwiftUI struct TrailingIconLabelStyle: LabelStyle { func makeBody(configuration: Configuration) -> some View { HStack { configuration.title configuration.icon } } } extension LabelStyle where Self == TrailingIconLabelStyle { static var trailingIcon: Self { Self() } } 확장에서 보면 where Self == TrailingI..