Text inside a VStack truncates when it's not supposed to in SwiftUI

后端 未结 3 1372
星月不相逢
星月不相逢 2021-01-01 19:29

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

相关标签:
3条回答
  • 2021-01-01 19:54

    Unfortunately It is a bug.

    There is a work around you can use to force it to recalculate elements but it's just a workaround and you must wait for the release and see if it fixed.

    Workaround

    Add a pair of spacer with the height of zero above and below the contents of ForEach.

    struct BugRepro: View {
    
        @State var length: Double = 1.0
    
        var body: some View {
            VStack {
                ForEach(0..<3) { i in
                    Spacer().frame(height: 0)
                    BugReproElement(index: i)
                    Spacer().frame(height: 0)
                }.frame(width: UIScreen.main.bounds.width * CGFloat(length))
    
                Slider(value: self.$length, in: 0.0...1.0)
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 20:02

    I figured out how to get the text to render properly.

    Applying the answer here works: Views compressed by other views in SwiftUI VStack and List

    The key is to ensure that the .fixedSize() gets added before .frame()

    No Spacer() needed!

    0 讨论(0)
  • 2021-01-01 20:08

    I had to add .fixedSize(horizontal: false, vertical: true) AND .padding() before all my text would stop truncating (show in view properly) I also have a frame tag for a separate vstack (I have 2 separate vstacks in the view)

    0 讨论(0)
提交回复
热议问题