问题
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