Skip to content

Instantly share code, notes, and snippets.

@gabrielribeiro
Created April 10, 2025 00:31
Show Gist options
  • Select an option

  • Save gabrielribeiro/fa332aae06f0e09853c4b07f5fd36ff1 to your computer and use it in GitHub Desktop.

Select an option

Save gabrielribeiro/fa332aae06f0e09853c4b07f5fd36ff1 to your computer and use it in GitHub Desktop.

Revisions

  1. gabrielribeiro created this gist Apr 10, 2025.
    47 changes: 47 additions & 0 deletions ContentView.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    import SwiftUI
    import ScrollKit

    struct ContentView: View {

    @State private var headerVisibleRatio = 1.0

    var body: some View {
    NavigationView {
    ScrollViewWithStickyHeader(
    header: header,
    headerHeight: 300,
    onScroll: handleScrollOffset
    ) {
    LazyVStack(spacing: 0) {
    ForEach(1...100, id: \.self) { item in
    VStack(spacing: 0) {
    Text("Item \(item)")
    .padding()
    .frame(maxWidth: .infinity, alignment: .leading)
    Divider()
    }
    }
    }
    }
    }
    }

    private func header() -> some View {
    ZStack {
    Color.blue
    .opacity(headerVisibleRatio)

    Color.red
    .opacity(1 - headerVisibleRatio)

    VStack {
    Text("Blue is header, red is preview (shouldn't be visible when scrolled)")
    }
    .opacity(headerVisibleRatio)
    }
    }

    private func handleScrollOffset(_ offset: CGPoint, headerVisibleRatio: CGFloat) {
    self.headerVisibleRatio = headerVisibleRatio
    }
    }