Well, honestly, I did it, because I needed it, and only then looked around and did not find anything on SO native in SwiftUI, so wanted to share. Thus this is just a self-answered question.
Initially I needed sticky stretchable sticky header for lazy content dependent only on ScrollView.
Later (after I got my solution) I found this one on Medium, but I don't like it (and would not recommend at least as-is), because:
- overcomplicated (many unneeded code, many unneeded calculations)
- depends (and joins) with safe area only, so limited applicability
- based on
offset(I don't like to use offset, because of its inconsistency with layout, etc.) - it is not sticky and to make it sticky it is needed even more code
So, actually all this text was just to fulfil SO question requirements - who knows me here knows that I don't like to type many text, it is better to type code , in short - my approach is below in answer, maybe someone find it useful.
Initial code which SwiftUI gives us for free
ScrollView {
LazyVStack(spacing: 8, pinnedViews: [.sectionHeaders]) {
Section {
ForEach(0...100) {
Text("Item \($0)")
.frame(maxWidth: .infinity, minHeight: 60)
}
} header: {
Image("picture").resizable().scaledToFill()
.frame(height: 200)
}
}
}
Header is sticky by scrolling up, but not when down (dragged with content), and it is not stretchable.
