实例化包含 UIScrollView 的 Storyboard 时 UIKit 崩溃

UIKit crash when instantiating a Storyboard containing a UIScrollView

各位,

我在使用 UIKit 时遇到了一个奇怪的问题,我无法在每次应用程序启动时重现该问题。问题很简单:我有一个主 ViewController 和一个包含 UIScrollView 的子 ViewController。你可以看到它如下图:

我添加子控制器的代码非常简单:

private func configureKeyboard() {
    guard let keyboardViewController = keyboardViewController else { return }

    addChild(keyboardViewController)

    keyboardContainerView.addSubview(keyboardViewController.view)
    keyboardViewController.didMove(toParent: self)
    keyboardViewController.view.translatesAutoresizingMaskIntoConstraints = false

    keyboardContainerView.topAnchor.constraint(equalTo: keyboardViewController.view.topAnchor, constant: 0).isActive = true
    keyboardContainerView.trailingAnchor.constraint(equalTo: keyboardViewController.view.trailingAnchor, constant: 0).isActive = true
    keyboardContainerView.bottomAnchor.constraint(equalTo: keyboardViewController.view.bottomAnchor, constant: 0).isActive = true
    keyboardContainerView.leadingAnchor.constraint(equalTo: keyboardViewController.view.leadingAnchor, constant: 0).isActive = true
}

但是 keyboardContainerView.addSubview(keyboardViewController.view) 行似乎导致了崩溃:

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named _UIScrollerImpContainerView because no class named _UIScrollerImpContainerView was found; the class needs to be defined in source code or linked in from a library (ensure the class is part of the correct target)'

看来 scrollView 是崩溃的原因。堆栈显示它似乎落在 init(coder:).

这是我实例化 KeyboardController 的方式。控制器本身在 scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions):

期间在协调器上启动
class func controller(dataSource: KeyboardDataSource,
                      stackEditor: StackEditor,
                      onOperationTouch: @escaping (_ operation: CalculatorOperation) -> Void,
                      onShouldPresentSettings: @escaping () -> Void) -> KeyboardViewController {
    let controller: KeyboardViewController = UIStoryboard(name: "KeyboardViewController", bundle: nil).instantiateInitialViewController() as! KeyboardViewController
    controller.dataSource = dataSource
    controller.stackEditor = stackEditor
    controller.didTouchOperation = onOperationTouch
    controller.shouldPresentSettings = onShouldPresentSettings
    return controller
}

这次崩溃让我想起了我们在 Xcode 的早期版本上使用 UITextView 时发生的崩溃,这可能会导致崩溃。我在 viewDidLoad() 上给 configureKeyboard 打电话。将它放在 viewDidAppear() 上并不能解决问题。我还应该说,该应用程序启用了 Catalyst。但我只在 iOS 应用程序上看到此崩溃。在 Mac 上完美运行。 有时,清理构建文件夹并删除应用程序似乎可以解决问题。但我害怕在 AppStore 上部署一个版本时出现这样的随机崩溃。这也发生在真实设备上。所以可能不是模拟器问题。 此外,KeyboardController 的 Storyboard 文件已正确关联到 iOS 应用程序目标。 Apple 开发者论坛上没有任何内容。我没主意了。

有人遇到过一些熟悉的问题吗?

在此先感谢您的帮助。

经过多次测试,我注意到显示具有 Mac 催化剂特征的故事板导致了问题。编译应用程序时,我不得不将故事板切换为 iPhone 或 iPad 特征。之后再也没有这个问题。