Skip to content

Instantly share code, notes, and snippets.

@zef
Last active December 9, 2022 04:56
Show Gist options
  • Select an option

  • Save zef/4192184f86bcac855d27a8e4e25d6510 to your computer and use it in GitHub Desktop.

Select an option

Save zef/4192184f86bcac855d27a8e4e25d6510 to your computer and use it in GitHub Desktop.
A reusable view where you can include content in a scroll view, with a footer that stays on the bottom of the screen until there is enough content to push it down further.
// Created by Zef Houssney on 12/8/22.
//
import SwiftUI
struct StickyFooterScrollView<Content, Footer>: View where Content: View, Footer: View {
let mainView: Content
let footer: Footer
init(@ViewBuilder mainView: () -> Content, @ViewBuilder footer: () -> Footer) {
self.mainView = mainView()
self.footer = footer()
}
var body: some View {
GeometryReader { proxy in
ScrollView {
VStack(spacing: 0) {
mainView
Spacer()
footer
}
.frame(maxWidth: .infinity, minHeight: proxy.size.height, alignment: .top)
}
}
}
}
struct StickyFooterScrollView_Previews: PreviewProvider {
static var previews: some View {
StickyFooterScrollView {
VStack {
ForEach(0...20, id: \.self) { index in
Text("\(index)")
}
}
} footer: {
let icons = ["circle.hexagongrid.circle.fill", "shuffle.circle.fill", "mic.circle.fill", "info.circle.fill"]
HStack(spacing: 28) {
ForEach(icons, id: \.self) { icon in
Image(systemName: icon)
}
}
.font(.title)
}
}
}
@zef
Copy link
Copy Markdown
Author

zef commented Dec 8, 2022

sticky footer

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