I am making an attempt to create sticky header with guide scroll to performance (eg. classes bar permitting person to maneuver to chosen part). I’ve points with one a part of that functionallity. Whereas ScrollViewReader
and proxy.scrollTo
works as meant; it retains scrolling underneath sticky header as a substitute of beneath it. Picture one reveals the way it works, picture two reveals what is predicted. As you possibly can see swiftUI principally ignores existence of header, treating it as separate Z layer.
I’d love answer to work on iOS 15.
Code to breed instance:
import SwiftUI
struct ContentView: View {
var physique: some View {
VStack {
ScrollViewReader(content material: { proxy in
ScrollView {
LazyVStack(spacing: 0, pinnedViews: [.sectionHeaders]) {
Picture(systemName: "globe")
.imageScale(.massive)
.foregroundStyle(.tint)
.onTapGesture {
proxy.scrollTo(sections[2].hashValue, anchor: .high)
}
Part {
NestedSectionsView()
.body(maxWidth: .infinity)
} header: {
Header().opacity(0.5)
}
}
}
})
}
.padding()
}
}
#Preview {
ContentView()
}
let sections = [
SectionData(header: "First Section", items: Array(repeating: "Item 1", count: 5)),
SectionData(header: "Second Section", items: Array(repeating: "Item 2", count: 5)),
SectionData(header: "Third Section", items: Array(repeating: "Item 3", count: 10))
]
struct NestedSectionsView: View {
var physique: some View {
LazyVStack(spacing: 10) {
ForEach(sections, id: .hashValue) { part in
SectionView(part: part)
}
}
.padding()
.navigationBarTitle("Nested Sections")
}
}
struct SectionData: Identifiable, Hashable {
let id = UUID()
let header: String
let objects: [String]
func hash(into hasher: inout Hasher) {
hasher.mix(id)
}
}
struct SectionView: View {
let part: SectionData
var physique: some View {
VStack(alignment: .main, spacing: 3) {
Textual content(part.header)
.font(.headline)
.body(top: 50)
.body(maxWidth: .infinity)
.background(Coloration.inexperienced)
.id(part.hashValue)
ForEach(part.objects, id: .self) { merchandise in
Textual content(merchandise)
.body(top: 140)
.body(maxWidth: .infinity)
.background(Coloration.purple)
}
}
}
}
struct Header: View {
var physique: some View {
VStack {
Picture(systemName: "picture")
.resizable()
.aspectRatio(contentMode: .match)
.body(width: UIScreen.primary.bounds.width, top: 100)
.padding()
Textual content("Instance Textual content")
.font(.title)
.foregroundColor(.black)
.body(width: UIScreen.primary.bounds.width, top: 100, alignment: .middle)
}.background(Coloration.yellow)
}
}
How does it work now (when tapping scroll to id of part 3)
How does I would love it to work (scrolls so part title is seen, and never lined by sticky header)