Get CGRect of View

前端 未结 1 895
既然无缘
既然无缘 2020-12-07 05:12

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

Like this:

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


        
相关标签:
1条回答
  • 2020-12-07 05:35

    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))
    
    0 讨论(0)
提交回复
热议问题