Saturday, September 24, 2022
HomeiOS Developmentios - SwiftUI: How would I make stretchable sticky header?

ios – SwiftUI: How would I make stretchable sticky header?

Nicely, actually, I did it, as a result of I wanted it, and solely then regarded round and didn’t discover something on SO native in SwiftUI, so needed to share. Thus that is only a self-answered query.

Initially I wanted sticky stretchable sticky header for lazy content material dependent solely on ScrollView.

Later (after I received my answer) I discovered this one on Medium, however I do not prefer it (and wouldn’t suggest a minimum of as-is), as a result of:

  1. overcomplicated (many unneeded code, many unneeded calculations)
  2. relies upon (and joins) with protected space solely, so restricted applicability
  3. primarily based on offset (I do not like to make use of offset, due to its inconsistency with structure, and so on.)
  4. it isn’t sticky and to make it sticky it’s wanted much more code

So, really all this textual content was simply to fulfil SO query necessities – who is aware of me right here is aware of that I do not prefer to kind many textual content, it’s higher to kind code 😀, in brief – my strategy is beneath in reply, perhaps somebody discover it helpful.

Preliminary code which SwiftUI provides us at no cost

ScrollView {
    LazyVStack(spacing: 8, pinnedViews: [.sectionHeaders]) {
        Part {
            ForEach(0...100) {
                Textual content("Merchandise ($0)")
                    .body(maxWidth: .infinity, minHeight: 60)
        } header: {
               .body(peak: 200)

Header is sticky by scrolling up, however not when down (dragged with content material), and it isn’t stretchable.



Please enter your comment!
Please enter your name here

Most Popular