我有一个List包含TextEditor
struct ContentView: View {
@State var text: String = "test"
var body: some View {
List((1...10), id: \.self) { _ in
TextEditor(text: $text)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它的项目并没有随着TextEditor. 我试过.fixedSize()修饰符但没有运气。我在这里缺少什么?
Moj*_*ini 20
您可以Text在 a 中使用 invisibleZStack使其动态化。
struct ContentView: View {
@State var text: String = "test"
var body: some View {
List((1...10), id: \.self) { _ in
ZStack {
TextEditor(text: $text)
Text(text).opacity(0).padding(.all, 8) // <- This will solve the issue if it is in the same ZStack
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,您应该考虑更改字体大小和其他属性以匹配 TextEditor
据我从视图层次结构中看到的TextEditor只是简单的包装器UITextView,没有更多要添加的内容,因此您可以进入该层并找到适合您需要的 UIKit 解决方案,或者...
这是在 SwiftUI 级别处理它的可能方法的演示(想法是使用Text视图作为包装行为的参考并TextEditor准确调整它)
使用 Xcode 12b / iOS 14 测试(添加红色边框以获得更好的可见性)
修改了您的视图:
struct ContentView: View {
@State var text: String = "test"
@State private var height: CGFloat = .zero
var body: some View {
List {
ForEach((1...10), id: \.self) { _ in
ZStack(alignment: .leading) {
Text(text).foregroundColor(.clear).padding(6)
.background(GeometryReader {
Color.clear.preference(key: ViewHeightKey.self, value: $0.frame(in: .local).size.height)
})
TextEditor(text: $text)
.frame(minHeight: height)
//.border(Color.red) // << for testing
}
.onPreferenceChange(ViewHeightKey.self) { height = $0 }
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:ViewHeightKey是我的其他解决方案中使用的首选项键,因此可以从那里获取
ForEach 和 GeometryReader:儿童的可变高度?
| 归档时间: |
|
| 查看次数: |
3681 次 |
| 最近记录: |