在 SwiftUI 中从 UIKit 更新 UIViewRepresentable 大小 [英] Update UIViewRepresentable size from UIKit in SwiftUI

查看:30
本文介绍了在 SwiftUI 中从 UIKit 更新 UIViewRepresentable 大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在父 SwiftUI VStack 中嵌入了一个具有可变高度 UITextView 的视图控制器,并且视图控制器将其框架调整为 viewDidLoad 之间的整个屏幕>viewDidLayoutSubviews.UITextView 仅扩展到自身内部文本的大小,并在父视图中居中.

我正在尝试将这个视图控制器添加到 VStack 中,并让它在外部表现得像其他 SwiftUI 组件一样 - 大小与其包含的内容完全一致 - 但它希望大小为整个屏幕减去其他 VStack 元素.

我可以在 didLayoutSubviews 中获得 UITextView 的正确大小并将其向上传递给 SwiftUI,在那里它可以正确设置 - 但我在哪里做呢?>

在下面的示例截图中,橙色是嵌入的UIView背景,绿色是UITextView,VStack看起来像这样:

 VStack {高亮VC()Text("点击并拖动以突出显示").foregroundColor(.gray).font(.caption)}

解决方案

在无法看到更多代码的情况下,很难说出最佳解决方案是什么,但纯粹基于您问题的这一部分..

<块引用>

我可以在 didLayoutSubviews 中获取 UITextView 的正确大小,并将其向上传递给 SwiftUI,在那里它可以正确设置 - 但我在哪里做呢?

我建议您将绑定属性传递给您的视图控制器,该属性可以设置为计算出的文本视图高度,这意味着包含您的 VStack 的视图将具有 @State 属性如下:

@State private var textViewHeight: CGFloat = 0

然后,您将在 HighlighterVC 上声明一个 @Binding 属性并添加这样的初始化程序:

@Binding var textViewHeight: CGFloat初始化(文本视图高度:绑定){self._textViewHeight = textViewHeight}

然后您将 textViewHeight 设置为您的 didLayoutSubviews 中计算出的高度,并向您的 HighlighterVC<添加 .frame 修饰符/code> 像这样:

VStack {HighlighterVC(textViewHeight: self.$textViewHeight).frame(高度:self.textViewHeight)Text("点击并拖动以突出显示").foregroundColor(.gray).font(.caption)}

就像我在回答开头所说的那样,这个解决方案(我认为可行,但由于我无法对其进行测试,因此我不能 100% 确定)基于您对您的想法需要.在没有看到更多代码的情况下,我无法判断这是否是最佳解决方案.

I'm embedding a view controller with variable-height UITextView inside a parent SwiftUI VStack and the view controller sizes it's frame to the whole screen between viewDidLoad and viewDidLayoutSubviews. The UITextView expands only to the size of the text inside itself and centers itself inside the parent view.

I'm trying to add this view controller in a VStack and have it behave externally like other SwiftUI components do - sized exactly to the content it contains - but it wants to be sized to the whole screen minus the other VStack elements.

I can get the correct size of the UITextView in didLayoutSubviews and pass it upwards to SwiftUI where it can be set properly - but where do I do that?

In the example screenshot below, the orange is the embedded UIView background, the green is the UITextView and the VStack looks like this:

    VStack {
        HighligherVC()
        Text("Tap and drag to highlight")
            .foregroundColor(.gray)
            .font(.caption)
    }

解决方案

Without being able to see more of your code, it's slightly difficult to say what the best solution would be, but based purely on this part of your question...

I can get the correct size of the UITextView in didLayoutSubviews and pass it upwards to SwiftUI where it can be set properly - but where do I do that?

I would suggest that you pass a binding property to your view controller that can be set to the calculated text view height, meaning that the view that contains your VStack would have a @State property like this:

@State private var textViewHeight: CGFloat = 0

You would then declare a @Binding property on your HighlighterVC and add an initializer like this:

@Binding var textViewHeight: CGFloat

init(textViewHeight: Binding<CGFloat>) {
    self._textViewHeight = textViewHeight
}

And then you would set textViewHeight to the calculated height in your didLayoutSubviews and add a .frame modifier to your HighlighterVC like this:

VStack {
    HighlighterVC(textViewHeight: self.$textViewHeight)
        .frame(height: self.textViewHeight)
    Text("Tap and drag to highlight")
        .foregroundColor(.gray)
        .font(.caption)
}

Like I said at the beginning of my answer, this solution (that I believe would work, but since I can't test it, I'm not 100% certain) is based on your thoughts about what it is that you need. Without seeing more code, it's impossible for me to say if this is the best solution.

这篇关于在 SwiftUI 中从 UIKit 更新 UIViewRepresentable 大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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