Skip to content

Instantly share code, notes, and snippets.

@mattcomi
Created August 12, 2021 03:38
Show Gist options
  • Save mattcomi/73450cd15f19755f737a5bd9b048b0f5 to your computer and use it in GitHub Desktop.
Save mattcomi/73450cd15f19755f737a5bd9b048b0f5 to your computer and use it in GitHub Desktop.
A placeholder that expands to fill the remaining screen height
// Created by Matt Comi on 12/8/21.
import SwiftUI
struct HeaderView: View {
var body: some View {
VStack(spacing: 6) {
Text("Stampy").font(.title).foregroundColor(.purple).fontWeight(.heavy)
Text("Your Digital Stamp Collection").font(.footnote).foregroundColor(.secondary)
}
.padding(32)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(UIColor.secondarySystemBackground))
.mask(RoundedRectangle(cornerRadius: 20, style: .continuous))
.padding(16)
}
}
struct PlaceholderView: View {
var body: some View {
Text("Your Stamp Collection is Empty").foregroundColor(.secondary)
}
}
struct HeightPreferenceKey: PreferenceKey {
typealias Value = CGFloat
static var defaultValue: Value = .zero
static func reduce(value _: inout CGFloat, nextValue _: () -> CGFloat) {}
}
struct ContentView: View {
@State var headerHeight: CGFloat = 0
var body: some View {
GeometryReader { screenGeo in
ScrollView {
VStack(spacing: 0) {
HeaderView()
.background(
GeometryReader { geo in
Color.clear.preference(key: HeightPreferenceKey.self, value: geo.size.height)
}
)
.onPreferenceChange(HeightPreferenceKey.self) { value in
headerHeight = value
}
PlaceholderView()
.frame(height: screenGeo.size.height - headerHeight)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment