Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Last active January 16, 2023 13:03
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IanKeen/a7253c06eb9978c8927981059c3ea7b1 to your computer and use it in GitHub Desktop.
Save IanKeen/a7253c06eb9978c8927981059c3ea7b1 to your computer and use it in GitHub Desktop.
SwiftUI relative frame
extension View {
func relative(width: CGFloat? = nil, height: CGFloat? = nil, alignment: Alignment = .center) -> some View {
Color.clear
.frame(maxWidth: width.map { _ in .infinity }, maxHeight: height.map { _ in .infinity })
.overlay(GeometryReader { proxy in
ZStack {
self.frame(
width: width.map { proxy.size.width * $0 },
height: height.map { proxy.size.height * $0 }
)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: alignment)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment