@@ -53,7 +53,6 @@ public struct StickyHeader<Content: View>: View {
5353
5454 public var body : some View {
5555
56- let offsetY : CGFloat = 0
5756
5857 let context = StickyHeaderContext (
5958 topMargin: topMargin,
@@ -64,22 +63,29 @@ public struct StickyHeader<Content: View>: View {
6463 Group {
6564 switch sizing {
6665 case . content:
66+
67+ let height = stretchingValue > 0
68+ ? baseContentHeight. map { $0 + stretchingValue }
69+ : nil
70+
71+ let baseContentHeight = stretchingValue > 0 ? self . baseContentHeight ?? 0 : nil
72+
6773 content ( context)
6874 . onGeometryChange ( for: CGSize . self, of: \. size) { size in
6975 if stretchingValue == 0 {
70- baseContentHeight = size. height
76+ self . baseContentHeight = size. height
7177 }
7278 }
7379 . frame (
74- height: baseContentHeight. map {
75- $0 + stretchingValue
76- }
80+ height: height
7781 )
78- . offset ( y: - stretchingValue)
82+ . offset ( y: stretchingValue > 0 ? - stretchingValue : 0 )
7983 // container
8084 . frame ( height: baseContentHeight, alignment: . top)
8185
8286 case . fixed( let height) :
87+
88+ let offsetY : CGFloat = 0
8389
8490 content ( context)
8591 . frame ( height: height + stretchingValue + offsetY)
@@ -253,3 +259,63 @@ private struct Pair: Equatable {
253259
254260 }
255261}
262+
263+
264+ #Preview( " dynamic height change " ) {
265+ @Previewable @State var itemCount : Int = 3
266+
267+ return ScrollView {
268+
269+ StickyHeader ( sizing: . content) { context in
270+
271+ ZStack {
272+
273+ Color . red
274+
275+ VStack ( spacing: 8 ) {
276+ ForEach ( 0 ..< itemCount, id: \. self) { index in
277+ Text ( " StickyHeader \( index + 1 ) " )
278+ . font ( . headline)
279+ }
280+ }
281+ . padding ( )
282+ . border ( Color . red)
283+ . frame ( maxWidth: . infinity, maxHeight: . infinity)
284+ . background ( . yellow)
285+ . background (
286+ Color . green
287+ . padding ( . top, - context. topMargin)
288+ )
289+ }
290+
291+ }
292+
293+ VStack ( spacing: 0 ) {
294+ HStack ( spacing: 16 ) {
295+ Button ( " Small (1) " ) {
296+ itemCount = 1
297+ }
298+ . buttonStyle ( . borderedProminent)
299+
300+ Button ( " Medium (3) " ) {
301+ itemCount = 3
302+ }
303+ . buttonStyle ( . borderedProminent)
304+
305+ Button ( " Large (5) " ) {
306+ itemCount = 5
307+ }
308+ . buttonStyle ( . borderedProminent)
309+ }
310+ . padding ( )
311+ . background ( Color . white)
312+
313+ ForEach ( 0 ..< 100 , id: \. self) { index in
314+ Text ( " Content \( index + 1 ) " )
315+ . frame ( maxWidth: . infinity)
316+ . padding ( )
317+ }
318+ }
319+ }
320+ }
321+
0 commit comments