Objective C 项目中的 SwiftUI - List 在预览中有效,而不是 运行

SwiftUI in ObjectiveC Project - List works in preview, not when it is run

我有一个非常大的 objective-c 项目,我正在尝试慢慢地合并 SwiftUI。我已经能够在 swiftUI classes 中成功实现一些从 Objective-c classes 调用的屏幕。

我现在面临的一个奇怪的错误是当应用程序 运行 时 List 不可见。它虽然在预览中可见。而且当我用 VStack 替换 List 时,当应用程序为 运行 时它看起来很好。如果我制作一个独立的 SwiftUI 应用程序和 运行 这段代码,那么它也 运行 没问题。仅当我从 objective-c class.

调用此 class 时才会出现问题

附加图片

Xcode Preview Screenshot

Device Screenshot

代码


import SwiftUI

struct InfoShareView: View {
    var body: some View {
             List{
                    Text("First Line")
                    Text("Second Line")
            }
            .padding()
            .background(Color.white)
        }

}

struct InfoShareView_Previews: PreviewProvider {
    static var previews: some View {
        InfoShareView()
    }
}

这里是对 InfoShareView 的调用


import UIKit
import SwiftUI

class AboutViewControllerSwift: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let swiftController = UIHostingController(rootView: InfoShareView())
        addChild(swiftController)

        swiftController.view.translatesAutoresizingMaskIntoConstraints  = false
        view.addSubview(swiftController.view)

        swiftController.view.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        swiftController.view.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

     }


}

这是来自 objective-c class

的电话
AboutViewControllerSwift *aboutViewController = [[AboutViewControllerSwift alloc] initWithNibName:nil bundle:nil];
self.nvgController=[[UINavigationController alloc]initWithRootViewController:aboutViewController];
self.window.rootViewController=self.nvgController;
[self.window makeKeyAndVisible];

我认为这是由于限制,请尝试使用以下

    ...
    view.addSubview(swiftController.view)

    swiftController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    swiftController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    swiftController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    swiftController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

将此添加到 viewdidload 的最后一行对我有用:

swiftController.view.frame = self.view.frame