gpt4 book ai didi

swift - 在 SwiftUI 中的列表中包含文本编辑器的动态行高

转载 作者:行者123 更新时间:2023-12-03 09:23:35 25 4
gpt4 key购买 nike

我有一个 List包含 TextEditor

struct ContentView: View {
@State var text: String = "test"

var body: some View {
List((1...10), id: \.self) { _ in
TextEditor(text: $text)
}
}
}
但它的项目并没有随着 TextEditor 的高度变化而增长.我试过 .fixedSize()没有运气的修改器。我在这里缺少什么?

最佳答案

您可以使用隐形 TextZStack使其动态化。

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
}
}
}
}
请注意,您应该考虑更改字体大小和其他属性以匹配 TextEditor Preview

关于swift - 在 SwiftUI 中的列表中包含文本编辑器的动态行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62620613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com