我有一个类似于下面代码的视图。出于某种原因,我的长文本被压缩了,而它本不该被压缩。为什么会这样?
struct ContentView: View {
var body: some View {
VStack {
ScrollView {
VStack(alignment: .leading, spacing: 32) {
HStack(alignment: .top, spacing: 8) {
Image(systemName: "hand.raised.fill")
.resizable()
.frame(width: 50, height: 50)
VStack(alignment: .leading) {
Text(shortText)
Text(longDescription)
.lineSpacing(4)
// this is being condensed
}
}
}
.padding([.leading, .trailing])
}
}
VStack {
// Custom Button goes here
Spacer()
}
.frame(height: 70)
}
}
.layoutPriority(1)
告诉布局系统,它应该优先考虑该视图的所需维度,而不是优先级较低的任何维度,因此它不会被压缩。
你的Text
被浓缩的原因是因为它在ScrollView
中。
在非滚动环境中,布局系统允许视图扩展,使用可用空间(即屏幕大小)作为限制。
在滚动上下文中,可用空间没有限制,因此允许视图扩展到最大大小可能导致某些视图具有无限大小(例如,Color.red
将尝试占用尽可能多的空间)。
这就是为什么你的Text
视图被压缩到它支持的最小空间(一行高度),而不是它支持的最大尺寸。
p。可能存在一个非layoutPriority(1)
的修饰符组合,允许Text
将其最小大小报告为显示其所有内容所需的大小。如果我有时间尝试找到它,我会更新这个答案。