Ios 在viewDidLoad外部创建UIHostingController时使用UIHostingController时,SwiftUI预览失败

Ios 在viewDidLoad外部创建UIHostingController时使用UIHostingController时,SwiftUI预览失败,ios,swift,swiftui,Ios,Swift,Swiftui,我有一些代码会导致SwiftUI预览失败,但会在模拟器和设备上成功运行。SwiftUI预览故障诊断信息为: PreviewUpdateTimedOutError: Updating took more than 5 seconds Updating a preview from ViewControllerPreviews in NestedTest.app (9218) took more than 5 seconds. 我将问题归结为少量代码: import SwiftUI import

我有一些代码会导致SwiftUI预览失败,但会在模拟器和设备上成功运行。SwiftUI预览故障诊断信息为:

PreviewUpdateTimedOutError: Updating took more than 5 seconds

Updating a preview from ViewControllerPreviews in NestedTest.app (9218) took more than 5 seconds.
我将问题归结为少量代码:

import SwiftUI
import UIKit

let createHostInInit = true

class ViewController: UIViewController {
    private var host: TestViewHost? = nil
    
    init() {
        if createHostInInit {
            host = TestViewHost()
        }

        super.init(nibName: nil, bundle: nil)
    }
    
    // Note: this is here to satisfy the compiler and to allow running this
    // in the simulator as part of a default new project.
    required init?(coder: NSCoder) {
        if createHostInInit {
            host = TestViewHost()
        }

        super.init(coder: coder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !createHostInInit {
            host = TestViewHost()
        }
        
        if let host = self.host {
            addChild(host)
            self.view.addSubview(host.view)
            host.didMove(toParent: self)
            host.view.frame = self.view.bounds
        }
    }
}

class TestViewHost: UIHostingController<TestView> {
    init() {
        super.init(rootView: TestView())
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

struct TestView: View {
    var body: some View {
        Text("Hi there!")
    }
}

struct UIViewControllerPreviewer: UIViewControllerRepresentable {
    let viewController: UIViewController
    
    init(viewController: UIViewController) {
        self.viewController = viewController
    }
    
    func makeUIViewController(context: Context) -> some UIViewController {
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        // Do nothing because this is only for previews
    }
}

struct ViewControllerPreviews: PreviewProvider {
    static var previews: some View {
        return UIViewControllerPreviewer(viewController: ViewController())
    }
}
导入快捷界面
导入UIKit
让createHostInInit=true
类ViewController:UIViewController{
私有变量主机:TestViewHost?=nil
init(){
如果createHostInInit{
host=TestViewHost()
}
super.init(nibName:nil,bundle:nil)
}
//注意:这是为了满足编译器的要求,并允许运行
//在模拟器中作为默认新项目的一部分。
必需初始化?(编码器:NSCoder){
如果createHostInInit{
host=TestViewHost()
}
super.init(编码器:编码器)
}
重写func viewDidLoad(){
super.viewDidLoad()
如果!createHostInInit{
host=TestViewHost()
}
如果let host=self.host{
addChild(主机)
self.view.addSubview(host.view)
host.didMove(toParent:self)
host.view.frame=self.view.bounds
}
}
}
类TestViewHost:UIHostingController{
init(){
super.init(rootView:TestView())
}
必需初始化?(编码器:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}
结构测试视图:视图{
var body:一些观点{
文本(“你好!”
}
}
结构UIViewControllerReviewer:UIViewControllerRepresentable{
让viewController:UIViewController
初始化(viewController:UIViewController){
self.viewController=viewController
}
func makeUIViewController(context:context)->一些UIViewController{
返回视图控制器
}
func updateUIViewController(uViewController:UIViewControllerType,context:context){
//不执行任何操作,因为这仅用于预览
}
}
结构ViewControllerPreviews:PreviewProvider{
静态var预览:一些视图{
返回UIViewControllerPreviewer(viewController:viewController())
}
}
如果
createHostInInit=true
,则预览失败。如果
createHostInInit=false
,则预览工作。在这两种情况下,UI在模拟器或设备上看起来都是正确的。因此,当在viewDidLoad()之前创建UIHostingController时,预览环境中的某些内容似乎变得古怪起来。 我是否错过了一些描述这些限制的UIHostingController文档?这是虫子吗? 可以在Xcode中通过创建一个新的iOS单视图项目并将此代码放到ViewController.swift中来测试这一点

谢谢