Get CGRect of View

I'm using a "RectGetter" to get the CGRect of a View.

Like this:

Text("Hello")
    .background(RectGetter(rect: self.$rect))


struct RectGetter: View {
    @Binding var rect: CGRect

    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) // crash here
        }

        return Rectangle().fill(Color.clear)
    }
}

But sometimes I get a crash when setting the rect.

Thread 0 name:
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x00000001a1ec5ec4 __pthread_kill + 8
1   libsystem_pthread.dylib         0x00000001a1de5724 pthread_kill$VARIANT$armv81 + 216 (pthread.c:1458)
2   libsystem_c.dylib               0x00000001a1d358c0 __abort + 112 (abort.c:147)
3   libsystem_c.dylib               0x00000001a1d35850 abort + 112 (abort.c:118)
4   AttributeGraph                  0x00000001cce56548 0x1cce25000 + 202056
5   AttributeGraph                  0x00000001cce2b980 0x1cce25000 + 27008
6   SwiftUI                         0x00000001d89ce9b4 $s7SwiftUI27_PositionAwareLayoutContextV10dimensionsSo6CGSizeVvg + 56 (<compiler-generated>:0)
7   SwiftUI                         0x00000001d8a43584 $sSo6CGSizeVIgd_ABIegr_TRTA + 24 (<compiler-generated>:0)
8   SwiftUI                         0x00000001d8a43a54 $s7SwiftUI13GeometryProxyV4sync33_4C4BAC551E328ACCA9CD3748EDC0CC3ALLyxSgxyXElFxAA9ViewGraphCXEfU_... + 92 (GeometryReader.swift:126)
9   SwiftUI                         0x00000001d8a43f20 $s7SwiftUI13GeometryProxyV4sync33_4C4BAC551E328ACCA9CD3748EDC0CC3ALLyxSgxyXElFxAA9ViewGraphCXEfU_... + 20 (<compiler-generated>:0)
10  SwiftUI                         0x00000001d8c4842c $s7SwiftUI16ViewRendererHostPAAE06updateC5Graph4bodyqd__qd__AA0cG0CXE_tlF + 80 (ViewRendererHost.swift:64)
11  SwiftUI                         0x00000001d8a438d4 $s7SwiftUI13GeometryProxyV5frame2inSo6CGRectVAA15CoordinateSpaceO_tF + 196 (GeometryReader.swift:125)
12  MyApp                           0x0000000102cf5c54 closure #1 in RectGetter.createView(proxy:) + 128 (RectGetter.swift:22)

Is there some more reliable way (not crashing) to get the CGRect of a View?


Solution 1:

I use such function. Works since Xcode 11.1

func rectReader(_ binding: Binding<CGRect>) -> some View {
    return GeometryReader { (geometry) -> AnyView in
        let rect = geometry.frame(in: .global)
        DispatchQueue.main.async {
            binding.wrappedValue = rect
        }
        return AnyView(Rectangle().fill(Color.clear))
    }
}

Usage the same

Text("Test").background(rectReader($rect))