因此,我制作了一个成功的动态 TextEditor,但是当我尝试组合视图时,它会与其他元素重叠,并且在您键入时不再扩展。我很确定这与 Geometry Reader 有关,但如果没有它,我不知道该怎么做。谢谢!
ScrollView {
GeometryReader { geometry in
VStack{
ZStack(alignment: .leading) {
Text(text).foregroundColor(.clear)
.padding(14)
.font(.custom("Times", size: 14))
.background(GeometryReader {
Color.clear.preference(key: viewheightkey3.self, value: $0.frame(in: .local).size.height)
})
.frame(width: geometry.size.width * 0.9)
TextEditor(text: $text)
.padding(6)
.foregroundColor(.white)
.frame(width: geometry.size.width * 0.9)
.frame(height: height)
.frame(minHeight: 100)
.background(Color.black)
}
.padding(20)
.onPreferenceChange(viewheightkey3.self) { height = $0 }
}
}
struct viewheightkey3: PreferenceKey {
static var defaultValue: CGFloat { 0 }
static func reduce(value: inout Value, nextValue: () -> Value) {
value = value + nextValue()
}
}