Created
June 9, 2021 06:38
-
-
Save xiangyu-sun/0ae9bfe441e6dd3cc2a7239537f732e1 to your computer and use it in GitHub Desktop.
GeometryReader that does not mess up with sizing!
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
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