Spacer inside ScrollView breaks layout
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()
Am I doing something wrong or is this expected? Really cool project otherwise!
That sure looks like a bug, thank you for reporting!
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
}