Swift UI:UIHostingController.view 不适合 iOS 13 的内容视图大小

Swift UI: UIHostingController.view is not fit to content view size at iOS 13

我要更新Swift UI根据通讯结果查看。 但是 UIHostingController.view 不适合 iOS 13 的 rootView 大小。 当我尝试使用下面的示例代码时,也会发生同样的事情。 我想将自调整 SwiftUI 视图添加到 UIStackView,但是 SwiftUI 视图与上一个和下一个视图重叠,因为这个问题. 我怎样才能避免这个问题?

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let object = SampleObject()
        let sampleView = SampleView(object: object)
        let hosting = UIHostingController(rootView: sampleView)
        hosting.view.backgroundColor = UIColor.green
        addChild(hosting)
        view.addSubview(hosting.view)
        hosting.view.translatesAutoresizingMaskIntoConstraints = false
        hosting.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        hosting.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        hosting.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        hosting.didMove(toParent: self)
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            object.test()
        }
    }
}


struct SampleView: View {
    @ObservedObject var object: SampleObject
    
    var body: some View {
        VStack {
            Text("test1").background(Color.blue)
            Text("test2").background(Color.red)
            if object.state.isVisibleText {
                Text("test2").background(Color.gray)
            }
        }
        .padding(32)
        .background(Color.yellow)
    }
}


final class SampleObject: ObservableObject {
    struct ViewState {
        var isVisibleText: Bool = false
    }
    
    @Published private(set) var state = ViewState()
    
    func test() {
        state.isVisibleText = true
    }
}

如果如下所示将子视图添加到UIStackView,Swift UI 视图的高度在iOS13 中不会改变。

iOS13(不正确)

iOS14(正确)

你还没有设置底部锚点,添加这行

hosting.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

另一个简单的方法是将框架设置为托管控制器视图并删除约束。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let object = SampleObject()
        let sampleView = SampleView(object: object)
        let hosting = UIHostingController(rootView: sampleView)
        hosting.view.frame = UIScreen.main.bounds //<---here
        hosting.view.backgroundColor = UIColor.green
        addChild(hosting)
        view.addSubview(hosting.view)
        hosting.didMove(toParent: self)
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            object.test()
        }
    }
}

仅限iOS 13

试试这个:

每次视图大小改变时,调用这个:

sampleView.view.removeFromSuperview()
let sampleView = SampleView(object: object)
let hosting = UIHostingController(rootView: sampleView)
view.addArrangedSubview(hosting.view)