I’m trying to use SwiftUI’s viewAligned scrollTargetBehavior for a ScrollView where scrollTargetLayout subviews are irregular in size.
Here’s an example, which I’ve simplified for the purpose of illustrating the simplest case:
VStack {
ScrollView {
LazyVStack {
ForEach(0..<100) { number in
Text(verbatim: String(number))
.font(.largeTitle)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: CGFloat.random(in: 100...300))
.background(Rectangle().fill(Color.green))
}
}
.scrollTargetLayout()
}
.scrollTargetBehavior(.viewAligned(limitBehavior: .always))
.background(.red)
}
.padding(.all, 16.0)
.background(.blue)
This works when minHeight is a fixed value (e.g. 100), but when sub-views vary in height (e.g. between 100 and 300 as above), the interaction works as expected for the second item (scrolling from the first item to the second item aligns the second item with the top of the scrollview), but not for the rest.
Curious if this is a know limitation of viewAligned, and if so, if it’s possible to do this using a custom implementation of .scrollTargetBehavior(_:)
I’ve searched broadly and haven’t been able to find mention of this specific issue of using irregularly sized sub-views for a viewAligned ScrollView anywhere. Thoughts, help, or a nudge in the right direction would be appreciated!