ARTICLE AD BOX
If I switch the axis of the scroll like this:
struct ScrollViewTest: View { @State private var axis: Axis.Set = .horizontal var body: some View { VStack { ScrollView(axis) { stack } Spacer() Button("Switch Axis") { axis = axis == .horizontal ? .vertical : .horizontal } } } var stack: some View { let stack = axis.contains(.horizontal) ? AnyLayout(HStackLayout()) : AnyLayout(VStackLayout()) stack { ForEach(0..<100) { i in Text("Item \(i)") } } } }It makes the scroll view to be scrollable in both directions after switching back to initial axis (here .horizontal) [Switches: .horizontal -> .vertical -> .horizontal].
To solve this I need to do if-else branching instead:
var body: some View { VStack { if axis == .horizontal { ScrollView(.horizontal) { stack } } else { ScrollView(.vertical) { stack } } Spacer() Button("Switch Axis") { axis = axis == .horizontal ? .vertical : .horizontal } } }Which solves the issue but changes the structural identity (It is now _ConditionalContent<ScrollView<ForEach>, ScrollView<ForEach>>).
Do any of you know why scroll view does this?
I have gone through this SO question: SwiftUI ScrollView vertical and horizontal scroll. The code I written also effectively does the same thing. What I want to know is the (reason/any possible explanation) behind this bug (I just hope someone would have uncovered it).
Explore related questions
See similar questions with these tags.
