The ScrollViewWithStickyHeader component allows you to create a header that stretches when pulled down and sticks to the top when scrolling up.
Configuration Options:
header: The view to be used as the header.headerHeight: The resting (default) height of the header.headerMinHeight: The minimum height the header can shrink to.headerStretch: A Boolean to enable or disable the stretching effect.contentCornerRadius: An optional corner radius applied to the content mask.onScroll: An optional closure (CGPoint, CGFloat) -> Void that provides the current scroll offset and the visibleHeaderRatio (the ratio of the header currently visible).
import SwiftUI
import ScrollKit
struct MyView: View {
@State
private var scrollOffset = CGPoint.zero
@State
private var visibleRatio = CGFloat.zero
var body: some View {
ScrollViewWithStickyHeader(
header: stickyHeader,
headerHeight: 250,
headerMinHeight: 150,
headerStretch: false,
contentCornerRadius: 20,
onScroll: handleScroll
) {
// Add your scroll content here, e.g. a `LazyVStack`
}
}
func handleScroll(_ offset: CGPoint, visibleHeaderRatio: CGFloat) {
self.scrollOffset = offset
self.visibleRatio = visibleHeaderRatio
}
func stickyHeader() -> some View {
ZStack {
Color.red
ScrollViewHeaderGradient() // By default a dark gradient
Text("Scroll offset: \(scrollOffset.y)")
}
}
}