Last active
March 9, 2021 02:05
-
-
Save paulhimes/677b61e4640da9e7ed3c590880d66dc4 to your computer and use it in GitHub Desktop.
OnLayout Tutorial 08
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
.background( | |
/// `proxy` will contain the size of the | |
/// title bar view. | |
GeometryReader { proxy in | |
layoutWatcher(proxy) | |
} | |
), | |
alignment: .top | |
) | |
.edgesIgnoringSafeArea(.all) | |
} | |
/// Returns a `View` with the side effect of using the proxy | |
/// value to update `titleBarHeight`. | |
func layoutWatcher(_ proxy: GeometryProxy) -> some View { | |
/// This updates the height whenever the view is updated. | |
titleBarHeight = proxy.size.height | |
return Color.clear | |
.onAppear { | |
/// This updates the height when the view first | |
/// appears. | |
titleBarHeight = proxy.size.height | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment