Created
May 24, 2023 20:25
-
-
Save kylebshr/93c8f5304170b2641d205b2f487c8543 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// ContentView.swift | |
// SafeArea | |
// | |
// Created by Kyle Bashour on 5/24/23. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
VStack { | |
Image(systemName: "globe") | |
.imageScale(.large) | |
.foregroundColor(.accentColor) | |
Text("Hello, world!") | |
} | |
.padding() | |
.frame(maxHeight: .infinity) | |
.bottom(minimumPadding: 20) { | |
Text("Hello") | |
} | |
} | |
} | |
struct MinimumBottomPadding<BottomContent: View>: ViewModifier { | |
@State private var bottomSafeAreaInset: CGFloat = 0 | |
var bottomContent: BottomContent | |
var minimumPadding: CGFloat | |
init(minimumPadding: CGFloat, @ViewBuilder bottomContent: () -> BottomContent) { | |
self.minimumPadding = minimumPadding | |
self.bottomContent = bottomContent() | |
} | |
func body(content: Content) -> some View { | |
content.safeAreaInset(edge: .bottom) { | |
bottomContent | |
.padding(.bottom, max(minimumPadding - bottomSafeAreaInset, 0)) | |
.background { | |
GeometryReader { geometry in | |
Color.clear.onAppear { | |
self.bottomSafeAreaInset = geometry.safeAreaInsets.bottom | |
} | |
} | |
} | |
} | |
} | |
} | |
extension View { | |
func bottom(minimumPadding: CGFloat, @ViewBuilder content: () -> some View) -> some View { | |
modifier(MinimumBottomPadding(minimumPadding: minimumPadding, bottomContent: content)) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
ContentView() | |
.previewDevice("iPhone SE (3rd generation)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment