summaryrefslogtreecommitdiff
path: root/Jel/Views/Utility/StickyHeaderView.swift
blob: b3bfd029d3081e524f96a4ff4fb3fa7fc43b1bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
//  StickyHeaderView.swift
//  Jel
//
//  Created by zerocool on 12/24/23.
//

import SwiftUI

struct StickyHeaderView<Content: View>: View {
  
  var minHeight: CGFloat
  var content: Content
  
  init(minHeight: CGFloat = 200, @ViewBuilder content: () -> Content) {
    self.minHeight = minHeight
    self.content = content()
  }
  
  var body: some View {
    GeometryReader { geo in
      if(geo.frame(in: .global).minY <= 0) {
        content
          .frame(width: geo.size.width, height: geo.size.height, alignment: .center)
      } else {
        content
          .offset(y: -geo.frame(in: .global).minY)
          .frame(width: geo.size.width, height: geo.size.height + geo.frame(in: .global).minY)
      }
    }
    .frame(minWidth: UIScreen.current?.bounds.width ?? 200, minHeight: minHeight)
  }
}

//#Preview {
//  StickyHeaderView {
//    Text("Test")
//  }
//}