ScrollView doesn't set contentInset
ryanmalesic opened this issue · 3 comments
ryanmalesic commented
import SwiftUI
import SwiftUIIntrospect
struct TestView: View {
var body: some View {
ScrollView {
ForEach(0..<300, id :\.self) { i in
Text(i.formatted())
.padding()
}
}
.introspect(.scrollView, on: .iOS(.v17)) {
$0.contentInset = UIEdgeInsets(top: 300, left: 0, bottom: 300, right: 0)
}
}
}
#Preview {
TestView()
}
This code results in a scrollview with the insets not visible. Calling print($0.contentInset)
DOES show the insets being set. Am I just doing something wrong here?
davdroman commented
Yeah, SwiftUI insists on taking over or ignoring certain properties, especially ones that can be controlled via built-in modifiers.
If you're exclusively targeting iOS 17, there's a new modifier to set content margins natively:
ScrollView {
ForEach(0..<300, id: \.self) { i in
Text(i.formatted())
.padding()
}
}
.contentMargins([.top, .bottom], 300, for: .scrollContent)
.contentMargins([.top, .bottom], 300, for: .scrollIndicators)
If you're targeting below iOS 17, you're going to have to use a hack such as https://stackoverflow.com/a/67327637/1922543.
davdroman commented
Closing due to inactivity but feel free to re-open to keep the convo going.