How to find the frame of a swiftui uiviewrepresentable

前提是你 提交于 2020-06-16 07:44:06

问题


I'm trying to wrap a custom subclass of UILabel in UIViewRepresentable to use it in Swiftui. I'm using .sizeToFit and printing the frame, and it looks right while it's in the wrapper:


func makeUIView(context: Context) -> CustomUILabel {
    let view = CustomUILabel()
    view.customProperty = model.customProperty
    view.sizeToFit()
    print(model.latex,view.frame.size) // this prints the correct size, how to propagate?
    return view
  }

but when I run this in a Vstack, it draws the UIViewRepresentable with the maximum space possible.

var body: some View {
    GeometryReader{ geometry in
        VStack(spacing: 0){
            Rectangle()
                .fill(Color.red)
                .frame( height: geometry.size.height/2 - 5 + self.draggedOffset.height)
            Rectangle()
                .fill(Color.orange)
                .frame(height: 10)
            custonView(model:self.model)
            Spacer()
    }
}

Is there a way to propagate the size of the UIView to its parent, similar to how you use preference keys on a native swiftui view?


回答1:


It is due to use of GeometryReader

Try to use

custonView(model:self.model)
    .fixedSize()


来源:https://stackoverflow.com/questions/59573879/how-to-find-the-frame-of-a-swiftui-uiviewrepresentable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!