如何找到SwiftUI UIViewRepresentable的框架 [英] How to find the frame of a SwiftUI UIViewRepresentable

查看:198
本文介绍了如何找到SwiftUI UIViewRepresentable的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将UILabel的自定义子类包装在UIViewRepresentable中,以在SwiftUI中使用它.我正在使用.sizeToFit并打印框架,当它在包装器中时看起来很正确:

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
}

但是当我在VStack中运行它时,它将以最大可能的空间绘制UIViewRepresentable.

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()
    }
}

是否有一种方法可以传播UIView的大小到其父级,类似于您在本机SwiftUI视图上使用首选项键的方式?

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?

推荐答案

这是由于使用了GeometryReader

尝试使用

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

这篇关于如何找到SwiftUI UIViewRepresentable的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆