// // LoginView.swift // BeReal // // Created by Javier Cicchelli on 30/11/2022. // Copyright © 2022 Röck+Cöde. All rights reserved. // import SwiftUI struct LoginView: View { // MARK: States @State private var topPadding: CGFloat = 0 // MARK: Body var body: some View { ScrollView( .vertical, showsIndicators: false ) { Container() .padding(.horizontal, 24) .padding(.top, topPadding) } .background(Color.red) .ignoresSafeArea() .overlay(ViewHeightGeometry()) .onPreferenceChange(ViewHeightPreferenceKey.self) { height in topPadding = height * 0.25 } } } // MARK: - Views fileprivate extension LoginView { struct Container: View { @State private var username: String = .empty @State private var password: String = .empty @State private var errorMessage: String? var body: some View { VStack(spacing: 24) { Text("My NFS") .font(.largeTitle) .fontWeight(.bold) LoginForm( username: $username, password: $password, errorMessage: $errorMessage ) Button { // ... } label: { Text("Log in") .font(.body) .fontWeight(.semibold) .padding(.vertical, 8) .padding(.horizontal, 32) .background( Capsule() .foregroundColor(.white) ) } } } } struct ViewHeightGeometry: View { var body: some View { GeometryReader { proxy in Color.clear.preference( key: ViewHeightPreferenceKey.self, value: proxy.size.height + proxy.safeAreaInsets.top + proxy.safeAreaInsets.bottom ) } } } } // MARK: - Preference keys struct ViewHeightPreferenceKey: PreferenceKey { static var defaultValue: CGFloat = 0 static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { value = nextValue() } } // MARK: - Previews struct LoginView_Previews: PreviewProvider { static var previews: some View { LoginView() } }