Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xiangyu-sun/0ae9bfe441e6dd3cc2a7239537f732e1 to your computer and use it in GitHub Desktop.
Save xiangyu-sun/0ae9bfe441e6dd3cc2a7239537f732e1 to your computer and use it in GitHub Desktop.
GeometryReader that does not mess up with sizing!
import SwiftUI
//Nick Cook
//@cookednick​
struct PassiveGeometryReader<Content: View>: View {
init(@ViewBuilder content: @escaping (PassiveGeometryProxy) -> Content) {
self.content = content
proxy = PassiveGeometryProxy(size: .zero, safeAreaInsets: .zero)
}
var body: some View {
content(proxy)
.background {
GeometryReader { geometry in
Spacer(minLength: 0)
.onAppear { proxy.size = geometry.size }
.onAppear { proxy.safeAreaInsets = geometry.safeAreaInsets }
.onChange(of: geometry.size) { proxy.size = $0 }
.onChange(of: geometry.safeAreaInsets) { proxy.safeAreaInsets = $0 }
}
.allowsHitTesting(false)
}
}
@State private var proxy: PassiveGeometryProxy
private let content: (PassiveGeometryProxy) -> Content
}
struct PassiveGeometryProxy {
var size: CGSize,
safeAreaInsets: EdgeInsets
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment