在SwiftUI中不应将VStack中的文本截断 [英] Text inside a VStack truncates when it's not supposed to in SwiftUI
问题描述
我正在尝试在VStack
内创建一个简单的Text
堆栈,无论我做什么,即使我显式设置了lineLimit(nil)
,文本也会被截断而不是自动换行(尽管我知道这一点)现在是默认设置.
I'm trying to create a simple stack of Text
inside a VStack
, and no matter what I do, the text will truncate instead of wrap, even if I explicitly set lineLimit(nil)
(although I know this is the default now).
我已经尝试在VStack的第一个元素上设置layoutPriority(1)
,并且我还尝试了其他一些帖子所建议的设置frame(idealHeight: .greatestFiniteMagnitude)
,但似乎没有任何解决方法.
I've tried setting layoutPriority(1)
on the first element in the VStack, and I have also tried setting frame(idealHeight: .greatestFiniteMagnitude)
as some other posts have suggested, but nothing seems to fix the issue.
以下是该问题的视频:
以下是重现此问题的一些代码:
Here is some code that reproduces the issue:
import SwiftUI
struct BugRepro: View {
@State var length: Double = 1.0
var body: some View {
VStack {
ForEach(0..<3) { i in
BugReproElement(index: i)
}
.background(Color.gray3)
.frame(width: UIScreen.main.bounds.width * CGFloat(length))
Slider(value: $length, in: 0.0...1.0)
}
}
}
struct BugRepro_Previews: PreviewProvider {
static var previews: some View {
BugRepro()
}
}
struct BugReproElement: View {
var index: Int
var body: some View {
Text("iaush isuh siudh siudh isudh isudhdsiu sdiuh sdihs")
.foregroundColor(.gray7)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
}
}
这可能只是Xcode中的错误吗?我正在运行Beta 7
Could this just be a bug in Xcode? I'm running Beta 7
推荐答案
我想出了如何使文本正确呈现的方法.
I figured out how to get the text to render properly.
在此处应用答案有效:由SwiftUI VStack和列表中的其他视图压缩的视图
Applying the answer here works: Views compressed by other views in SwiftUI VStack and List
关键是要确保在.frame()
不需要Spacer()
!
这篇关于在SwiftUI中不应将VStack中的文本截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!