Make ScrollView content fill its parent in SwiftUI
A simple way for you, using frame(maxWidth: .infinity)
ScrollView(.vertical) {
VStack {
ForEach(0..<100) {
Text("Item \($0)")
}
}
.frame(maxWidth: .infinity)
}
Actually you don't need GeometryReader
anymore. ScrollView
has been refactored in Xcode beta 3. Now you can declare that you have a .horizontal
or .vertical
ScrollView
.
This makes the ScrollView
behave like it should, like any normal View
protocol.
Ex:
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach((1...10).reversed()) {
AnyViewYouWant(number: $0)
}
}
}
This will result in a view with the width of its parent scrolling horizontally. The height will be defined by the ScrollView's subviews height.