Skip to content

Commit 0d969c8

Browse files
authored
Fix StickyHeader (#50)
1 parent 742e060 commit 0d969c8

File tree

1 file changed

+82
-63
lines changed

1 file changed

+82
-63
lines changed

Sources/StickyHeader/StickyHeader.swift

Lines changed: 82 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,29 @@ public struct StickyHeader<Content: View>: View {
8686
// container
8787
.frame(height: height, alignment: .top)
8888
}
89-
}
90-
.onGeometryChange(
91-
for: CGRect.self,
92-
of: {
93-
$0.frame(in: .global)
94-
},
95-
action: { value in
96-
let minY = value.minY
97-
if minY >= 0, topMargin != minY {
98-
topMargin = minY
99-
}
89+
}.onGeometryChange(for: Pair.self, of: {
90+
Pair(
91+
minYInGlobal: $0.frame(in: .global).minY,
92+
minYInCoordinateSpace: $0.frame(in: .named(coordinateSpaceName)).minY
93+
)
94+
}, action: { pair in
95+
96+
self.stretchingValue = max(0, pair.minYInCoordinateSpace)
97+
98+
let minY = pair.minYInGlobal
99+
if minY >= 0, topMargin != minY {
100+
topMargin = minY - stretchingValue
100101
}
101-
)
102-
.onGeometryChange(
103-
for: CGFloat.self,
104-
of: {
105-
$0.frame(in: .named(coordinateSpaceName)).minY
106-
},
107-
action: { value in
108-
self.stretchingValue = max(0, value)
109-
})
110-
102+
})
103+
111104
}
112105
}
113106

107+
private struct Pair: Equatable {
108+
let minYInGlobal: CGFloat
109+
let minYInCoordinateSpace: CGFloat
110+
}
111+
114112
private let coordinateSpaceName = "app.muukii.stickyHeader.scrollView"
115113

116114
extension View {
@@ -123,33 +121,45 @@ extension View {
123121

124122
#Preview("dynamic") {
125123
ScrollView {
126-
127-
StickyHeader(sizing: .content) { context in
128-
129-
ZStack {
130-
131-
Color.red
132-
.padding(.top, -context.topMargin)
133-
134-
VStack {
135-
Text("StickyHeader")
136-
Text("StickyHeader")
137-
Text("StickyHeader")
138-
}
139-
.border(Color.red)
140-
.frame(maxWidth: .infinity, maxHeight: .infinity)
141-
// .background(.yellow)
124+
125+
Section {
126+
127+
ForEach(0..<100, id: \.self) { _ in
128+
Text("Hello World!")
129+
.frame(maxWidth: .infinity)
142130
}
143-
144-
}
145-
146-
ForEach(0..<100, id: \.self) { _ in
147-
Text("Hello World!")
148-
.frame(maxWidth: .infinity)
131+
132+
} header: {
133+
134+
StickyHeader(sizing: .content) { context in
135+
136+
ZStack {
137+
138+
Rectangle()
139+
.stroke(lineWidth: 10)
140+
.padding(.top, -context.topMargin)
141+
//
142+
143+
VStack {
144+
Text("StickyHeader")
145+
Text("StickyHeader")
146+
Text("StickyHeader")
147+
}
148+
.border(Color.red)
149+
.frame(maxWidth: .infinity, maxHeight: .infinity)
150+
// .background(.yellow)
151+
// .background(
152+
// Color.green
153+
// .padding(.top, -context.topMargin)
154+
//
155+
// )
156+
}
157+
}
149158
}
159+
.padding(.top, 20)
160+
150161
}
151162
.enableStickyHeader()
152-
.padding(.vertical, 100)
153163
}
154164

155165
#Preview("dynamic full") {
@@ -214,32 +224,41 @@ extension View {
214224
#Preview("fixed full") {
215225
ScrollView {
216226

217-
StickyHeader(sizing: .fixed(300)) { context in
227+
Section {
218228

219-
ZStack {
229+
ForEach(0..<100, id: \.self) { _ in
230+
Text("Hello World!")
231+
.frame(maxWidth: .infinity)
232+
}
233+
} header: {
220234

221-
Color.red
235+
StickyHeader(sizing: .fixed(300)) { context in
222236

223-
VStack {
224-
Text("StickyHeader")
225-
Text("StickyHeader")
226-
Text("StickyHeader")
227-
}
228-
.border(Color.red)
229-
.frame(maxWidth: .infinity, maxHeight: .infinity)
230-
// .background(.yellow)
231-
.background(
232-
Color.green
237+
ZStack {
238+
239+
Color.red
233240
.padding(.top, -context.topMargin)
241+
//
234242

235-
)
236-
}
243+
VStack {
244+
Text("StickyHeader")
245+
Text("StickyHeader")
246+
Text("StickyHeader")
247+
}
248+
.border(Color.red)
249+
.frame(maxWidth: .infinity, maxHeight: .infinity)
250+
// .background(.yellow)
251+
// .background(
252+
// Color.green
253+
// .padding(.top, -context.topMargin)
254+
//
255+
// )
256+
}
257+
}
237258
}
259+
.padding(.top, 20)
260+
238261

239-
ForEach(0..<100, id: \.self) { _ in
240-
Text("Hello World!")
241-
.frame(maxWidth: .infinity)
242-
}
243262
}
244263
.enableStickyHeader()
245264

0 commit comments

Comments
 (0)