gpt4 book ai didi

ios - 如何调整 UIHostingController 的大小以包装它的 SwiftUI View ?

转载 作者:行者123 更新时间:2023-12-01 21:59:05 30 4
gpt4 key购买 nike

我有一个基本的 SwiftUI 组件。

struct PopupModal: View {
var body: some View {
Text("Hello, World!")
}
}

struct PopupModal_Previews: PreviewProvider {
static var previews: some View {
PopupModal()
}
}

我有一个 UIViewController我想实例化这个组件并显示它。
    private let _popup = PopupModal()
private lazy var _popupController = UIHostingController(rootView: _popup)

override public func viewDidLoad() {
// ...
self.addChild(_popupController)
view.addSubview(_popupController.view)
_popupController.didMove(toParent: self)

}

override public func viewDidLayoutSubviews() {
// How do I make this automatic or as a function of the size of _popup?
let popupHeight = CGFloat(260)
_popupController.view.frame = CGRect(x: 15, y: view.bounds.height - popupHeight - 20, width: view.bounds.width - 30, height: popupHeight)
}
PopupModal显示,但我手动调整它的高度。怎么能
我根据内容的大小来调整大小?

最佳答案

您可以使用

  _popupController.view.sizeToFit()

使 View 最初适合其内在内容,并在添加到 super View 后使用自动布局约束根据需要对其进行布局。

关于ios - 如何调整 UIHostingController 的大小以包装它的 SwiftUI View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60663679/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com