Search code examples
iosswiftuixcode11

Cell row autosizing broken when add Rectangle() in SwiftUI


I'm having trouble positioning a simple Rectangle() in a horizontal stack. If I add it, the Text() stops resizing like this:

Fail

If I remove the Rectangle(), woks fine:

Good

I tried changing frame, relativeSize, layoutPriority and much more, but nothing works. I think that is a bug, but fails with any type of geometric types like Circle, RoundedRectangle, etc. On the other hand, with an Image() it works fine.

Any suggestion?

Thanks!


Solution

  • Just writing out of my head, could be wrong, but try to add rectangle in VStack so that it does not wrap cell around it.

    VStack {
        Rectangle()
        Spacer()
    }
    

    Let me know if it works.

    Edit*

    Had to try it out, and find a "kinda" solution, it may lead you to correct answer, you just have to position rectangle in top right corner. Make this your RowItem.

    ZStack {
            Rectangle()
                .foregroundColor(Color.red)
                .frame(width: 10, height: 10)
    
            Text("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.")
            .lineLimit(nil)
            .layoutPriority(999)
            .padding(.horizontal, 20)
        }