
SwiftUI의 TabView는 PageTabViewStyle을 제공한다. 다음 코드는 CoreData의 모델을 PageTabViewStyle로 만든 코드가 된다. indexViewStyle도 추가해서 하단에 인덱스뷰도 보이게 했다. struct ContentView: View { @Environment(\.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: true)], animation: .default) private var items: FetchedResults var body: some View { TabView { Fo..
Form에서 TextField가 있을 때, 스크롤 시 키보드는 감추는 예제이다. 먼저 키보드를 감추는 메서드를 View에 확장해서 쉽게 키보드를 감출 수 있게 한다. import Foundation import SwiftUI extension View { func hideKeyboard() { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) } } 그리고 Form에 simultaneousGesture 수정자를 추가하고, 드래그 제스처가 발생 시 키보드를 감추는 메서드를 호출한다. var body: some View { Form { ... } .simultaneou..

현재 스크롤 뷰에 포함된 TextEditor에서 줄바꿈이 발생하면 뷰는 커지지만 스크롤이 되지 않아서 자동 스크롤이 되도록 만들어야 한다. # TextEditor의 최소 높이 설정하기 먼저 알아야 할 것은 List 또는 ScrollView 안에 TextEditor를 넣으면 TextEditor의 높이는 최소가 된다. 그래서 frame으로 minHeight를 설정해야 한다. # TextEditor 크기를 구하는 방법 아직은 TextEditor의 크기를 얻는 메서드가 제공되지 않는다. 그래서 bacoground에 HStack같이 확장되는 뷰를 넣고 GeometryReader로 HStack의 사이즈를 측정하면 TextEditor의 크기를 얻을 수 있다. 다음은 TextEditor의 크기를 구하는 간단한 Swif..
TextEditor에는 PlaceHolder를 설정할 수 없다. 그래서 ZStack을 이용해서 TextEditor의 텍스트가 빈문자열인 경우, PlaceHolder로 사용할 Text를 보이게 하면 된다. ZStack(alignment: .topLeading) { if content.isEmpty { Text("content") .padding(7) .foregroundColor(Color(uiColor: .placeholderText)) } else { EmptyView() } TextEditor(text: $content) }

Form에서 이름과 콘텐츠를 가지는 경우가 너무 많아서 뷰로 만들어 보았다. 이때 @ViewBuilder를 사용해야 하는데, 구글링을 통해서 얻은 코드로 작동만 되도록 만들어서, 정확한 작동법은 알지 못한다. 나중에 시간내서 @ViewBuilder에 대해서 정리해야 겠다. import SwiftUI struct NamedContent: View { var name: String @ViewBuilder var content: () -> Content var body: some View { HStack { Text(name) .bold() Spacer() content() } } } struct NamedContent_Previews: PreviewProvider { static var previews: s..
iOS 16 이상 TextEditor(text: $text) .scrollContentBackground(.hidden) .background(.red) iOS 15 이하 struct ContentView: View { @State private var text = "" init() { UITextView.appearance().backgroundColor = .clear } var body: some View { List { TextEditor(text: $text) .background(.red) } } }

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은 그룹 색상이 ..