When a ScrollView or List is nested in a TabView, you can press on the tab button and the scroll view will scroll to top.
import SwiftUI
struct SwiftUIView: View {
let items = (1...100).map { "Item \($0)" }
var body: some View {
TabView {
Tab("home", systemImage: "house") {
ScrollView {
ForEach(items, id: \.self) { item in
Text(item)
.frame(maxWidth: .infinity, alignment: .center)
}
}
}
}
}
}
#Preview {
SwiftUIView()
}
But if we add a background to the ScrollView, the scroll to top gesture breaks.
import SwiftUI
struct SwiftUIView: View {
let items = (1...100).map { "Item \($0)" }
var body: some View {
TabView {
Tab("home", systemImage: "house") {
ScrollView {
ForEach(items, id: \.self) { item in
Text(item)
.frame(maxWidth: .infinity, alignment: .center)
}
}
// Set background on ScrollView.
.background(Color.red)
}
}
}
}
#Preview {
SwiftUIView()
}
I made a similar post on StackOverflow, but haven't been able to find a proper solution.
This feels like a bug of some sort in SwiftUI.