Skip to content

Instantly share code, notes, and snippets.

@paulhimes
Created March 9, 2021 02:01
Show Gist options
  • Save paulhimes/dc629fb0e6f1605167afe90e622d0353 to your computer and use it in GitHub Desktop.
Save paulhimes/dc629fb0e6f1605167afe90e622d0353 to your computer and use it in GitHub Desktop.
OnLayout Tutorial 11
struct MainView: View {
/// Controls the amount of padding above the scroll view's
/// content.
@State var titleBarHeight: CGFloat = 123
var body: some View {
ScrollView {
HStack {
Spacer()
Text("ScrollView Content")
.multilineTextAlignment(.center)
.foregroundColor(.white)
.background(Color.blue.brightness(0.2))
Spacer()
}
/// Pad the top of the content based on the value
/// of `titleBarHeight`.
.padding(.top, titleBarHeight)
}
.background(Color.blue)
.overlay(
TitleBar()
/// Update the `titleBarHeight` every time the
/// title bar view changes.
.onLayout { proxy in
titleBarHeight = proxy.size.height
},
alignment: .top
)
.edgesIgnoringSafeArea(.all)
}
}
struct TitleBar : View {
var body: some View {
HStack {
Text("Title")
.frame(alignment: .center)
.foregroundColor(.white)
Spacer()
}
.padding(
EdgeInsets(
top: 50,
leading: 16,
bottom: 16,
trailing: 16
)
)
.background(Color.red.opacity(0.5))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment