我正在尝试在UIViewRepresentable中包装UILabel的自定义子类,以便在Swiftui中使用它。我正在使用.sizeToFit并打印框架,当它在包装器中时看起来正确:
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
}
但是当我在Vstack中运行它时,它将以最大可能的空间绘制UIViewRepresentable。
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()
}
}
是否有一种方法可以将UIView的大小传播到其父级,类似于您在本机swiftui视图上使用首选项键的方式?
由于使用GeometryReader
尝试使用
custonView(model:self.model)
.fixedSize()