SwiftTUI icon indicating copy to clipboard operation
SwiftTUI copied to clipboard

Spacer inside ScrollView breaks layout

Open Amzd opened this issue 1 year ago • 2 comments

Putting a horizontal Spacer inside a ScrollView breaks layout.

ScrollView {
  VStack {
    HStack {
      Button("test ") {}
      Spacer() // this spacer inside scrollview is bad!
      Text("text i want to align right")
    }
  }.border(.blue)
}.border()

image

Am I doing something wrong or is this expected? Really cool project otherwise!

Amzd avatar Jan 19 '24 19:01 Amzd

That sure looks like a bug, thank you for reporting!

rensbreur avatar Jan 22 '24 22:01 rensbreur

Hello @Amzd, I believe I can provide a plausible explanation for the issue.

ScrollControl is proposing a width of 0 to its content. In your example, the HStackControl divides this proposed width among the button, the spacer, and the text. This results in the SpacerControl receiving a proposed width that is less than 0, which causes the HStackControl to shrink instead of expand.

I would say we could fix this by passing ScrollView's size as proposed size to its content:

override func layout(size: Size) {
            super.layout(size: size)
-            let contentSize = contentControl.size(proposedSize: .zero)
+            let contentSize = contentControl.size(proposedSize: size)
            contentControl.layout(size: contentSize)
            contentControl.layer.frame.position.line = -contentOffset
        }

hectr avatar Sep 29 '24 21:09 hectr