Hi everyone, I have the following issue that I have tried to tweak every possible modifier of ScrollView and still got the same result in iOS 26.
Description: Create a SwiftUI ScrollView with scrollTargetBehavior of paging, also create a bottom UI view below the ScrollView. If the starting index is not 0, the position of current page will be off with part of previous page shown above it. It only happens on iOS 26, not on iOS 18. Also if bottom UI view (text view in this case) is removed, it also works fine.
I want to see if there is a solution for it or it's an iOS 26 bug. Thanks!
import SwiftUI
struct ContentView: View {
@State private var currentPageIndex: Int? = 3
var body: some View {
VStack {
scrollView
Text("Bottom Bar")
.frame(maxWidth: .infinity)
.frame(height: 80)
.background(.red)
}
.background(.black)
}
@ViewBuilder
var scrollView: some View {
VerticalPagerView(
currentPageIndex: $currentPageIndex,
itemCount: 10,
content: Array(0...9).map { index in
content(for: index)
}
)
}
@ViewBuilder
private func content(for index: Int) -> some View {
// Empty view with random background color
Color(
red: Double((index * 25 + 0) % 255) / 255.0,
green: Double((index * 25 + 80) % 255) / 255.0,
blue: Double((index * 25 + 160) % 255) / 255.0
)
}
}
struct VerticalPagerView<Content: View>: View {
@Binding private var currentPageIndex: Int?
private let itemCount: Int
private let content: [Content]
init(
currentPageIndex: Binding<Int?>,
itemCount: Int,
content: [Content]
) {
self._currentPageIndex = currentPageIndex
self.itemCount = itemCount
self.content = content
}
var body: some View {
GeometryReader { geometryReader in
ScrollViewReader { reader in
ScrollView(.vertical) {
LazyVStack(spacing: 0) {
ForEach(0 ..< itemCount, id: \.self) { index in
content[index]
.id(index)
.containerRelativeFrame(.vertical, alignment: .center)
.clipped()
}
}
.frame(minHeight: geometryReader.size.height)
.scrollTargetLayout()
}
.scrollIndicators(.hidden)
.onAppear {
guard let currentPageIndex = currentPageIndex else { return }
reader.scrollTo(currentPageIndex, anchor: .center)
}
}
.scrollPosition(id: $currentPageIndex, anchor: .center)
.ignoresSafeArea()
.scrollTargetBehavior(.paging)
.onChange(of: currentPageIndex) { oldIndex, newIndex in
}
}
}
}