티스토리 뷰
현재 스크롤 뷰에 포함된 TextEditor에서 줄바꿈이 발생하면 뷰는 커지지만 스크롤이 되지 않아서 자동 스크롤이 되도록 만들어야 한다.
# TextEditor의 최소 높이 설정하기
먼저 알아야 할 것은 List 또는 ScrollView 안에 TextEditor를 넣으면 TextEditor의 높이는 최소가 된다. 그래서 frame으로 minHeight를 설정해야 한다.
# TextEditor 크기를 구하는 방법
아직은 TextEditor의 크기를 얻는 메서드가 제공되지 않는다. 그래서 bacoground에 HStack같이 확장되는 뷰를 넣고 GeometryReader로 HStack의 사이즈를 측정하면 TextEditor의 크기를 얻을 수 있다.
다음은 TextEditor의 크기를 구하는 간단한 SwiftUI 예제코드이다.
struct ContentView: View {
@State var text = ""
@State var size = CGSize(width: 0.0, height: 0.0)
var body: some View {
ScrollView {
VStack {
Text("Height * Width = \(size.height) * \(size.width)")
Text("-----")
ZStack {
TextEditor(text: $text)
.frame(minHeight: 50)
.fixedSize(horizontal: false, vertical: true)
.background(GeometryReader { proxy in
HStack {}
.onAppear { size = proxy.size }
.onChange(of: proxy.size) { _ in size = proxy.size }
})
}
Text("-----")
}
}
}
}
'SwiftUI > Views' 카테고리의 다른 글
[SwiftUI] TabView / PageTabViewStyle / ForEach (0) | 2023.09.17 |
---|---|
[SwiftUI] Form에서 스크롤 시 키보드 숨기기 (0) | 2023.09.14 |
[SwiftUI] TextEditor에 PlaceHolder 추가하기 (0) | 2023.09.11 |
[SwiftUI] @ViewBuilder (0) | 2023.08.26 |
[SwiftUI] TextEditor 배경색 변경하기 (0) | 2023.08.26 |
댓글