Skip to content

Instantly share code, notes, and snippets.

@tomasen
Created December 25, 2021 16:05
Show Gist options
  • Save tomasen/bf9163bab740a73c291895683be59d32 to your computer and use it in GitHub Desktop.
Save tomasen/bf9163bab740a73c291895683be59d32 to your computer and use it in GitHub Desktop.
SwiftUI RectGetter example
struct RectGetterDemoView: View {
@State private var canvasRect = CGRect()
var body: some View {
Text("Demo").background(RectGetter($canvasRect))
}
}
struct RectGetter: View {
@Binding var rect: CGRect
init(_ rect: Binding<CGRect>) {
_rect = rect
}
var body: some View {
GeometryReader { proxy in
self.createView(proxy: proxy)
}
}
func createView(proxy: GeometryProxy) -> some View {
DispatchQueue.main.async {
self.rect = proxy.frame(in: .global)
}
return Rectangle().fill(Color.clear)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment