Skip to content

Instantly share code, notes, and snippets.

@yspreen
Last active May 29, 2025 15:54
Show Gist options
  • Save yspreen/f618ee839adc31afdbb89e9f21eca2d1 to your computer and use it in GitHub Desktop.
Save yspreen/f618ee839adc31afdbb89e9f21eca2d1 to your computer and use it in GitHub Desktop.
Sticky Section Header
/**
* ContentView.swift
* StickyTest
*
* Created by Nick Spreen (spreen.co) on 5/28/25.
*
*/
import SwiftUI
struct Section: Identifiable, Hashable {
let id = UUID()
let title: String
let items: [Item]
struct Item: Identifiable, Hashable {
let id = UUID()
let title: String
}
}
let hundredExampleSections = (0..<100).map { index in
Section(title: "Section \(index + 1)", items: (0..<10).map { index in
Section.Item(title: "Item \(index + 1)")
})
}
struct ContentView: View {
var body: some View {
ScrollView(.horizontal) {
LazyHStack(spacing: 0) {
ForEach(hundredExampleSections) { section in
SectionView(section: section)
}
}
}
}
}
struct SectionView: View {
let section: Section
let padding: CGFloat = 4
@State var sectionMinX: CGFloat = 0
@State var sectionMaxX: CGFloat = 0
@State var labelMinX: CGFloat = 0
@State var labelWidth: CGFloat = 0
var offsetX: CGFloat {
min(max(-labelMinX, 0) + padding, sectionMaxX - sectionMinX - labelWidth - 3 * padding)
}
var body: some View {
HStack(spacing: padding) {
Rectangle()
.frame(width: 1)
.frame(height: 1)
.opacity(0)
.overlay {
GeometryReader { geo in
Color.clear.onChange(of: geo.frame(in: .global).minX, initial: true) {
sectionMinX = $1
}
}
}
VStack(alignment: .leading, spacing: 0) {
Text(section.title)
.offset(x: offsetX)
.overlay {
GeometryReader { geo in
Color.clear.onChange(of: geo.frame(in: .global).minX, initial: true) {
labelMinX = $1
}.onChange(of: geo.frame(in: .global).width, initial: true) {
labelWidth = $1
}
}
}
HStack(spacing: 12) {
ForEach(section.items) { item in
Text(item.title)
}
}
}
Rectangle()
.frame(width: 1)
.frame(height: 1)
.opacity(0)
.overlay {
GeometryReader { geo in
Color.clear.onChange(of: geo.frame(in: .global).maxX, initial: true) {
sectionMaxX = $1
}
}
}
}.overlay(alignment: .trailing) {
Rectangle()
.frame(width: 1)
.opacity(0.5)
}
}
}
#Preview {
ContentView()
}

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org/

@alpennec
Copy link

It seems the SectionView offset parameter is not used, is it?

@yspreen
Copy link
Author

yspreen commented May 28, 2025

It seems the SectionView offset parameter is not used, is it?

that's right! removed it

@alpennec
Copy link

Thank you.

I'm wondering how the GeometryProxy can report the frame(in: .global).minX with LazyHStack. If it's lazy, the width of each section is not known 🤔

@yspreen
Copy link
Author

yspreen commented May 29, 2025

Thank you.

I'm wondering how the GeometryProxy can report the frame(in: .global).minX with LazyHStack. If it's lazy, the width of each section is not known 🤔

every section is only keeping track of its own positions and sizes and values.

Every section is part of a lazy container so they are initiated and removed as needed

@alpennec
Copy link

alpennec commented May 29, 2025

Ah yeah my bad, the global is referring to the whole device screen, not the size of all the scroll view content. Thank you for helping!

If the view is not full width, like if it has horizontal padding, maybe we should use the local coordinate space instead?

@yspreen
Copy link
Author

yspreen commented May 29, 2025

All position values are used for relative offsets so it doesn’t matter. The one big exception is the 0 for the left boundary. If you have the view anywhere but on the very left edge of the screen, it has to be replaced with the x position of the parent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment