I faced the same issue. Project was legacy UIKit app and I was adding new features with SwiftUI. It would run on the phone and ipad emulator but crash with this error.
Fix:
I changed the navigation view in my view files to a scrollview and removed all the navigation buttons and hide overloads. I then used a UIHostingController and use the UIKit Navigation components and was able to build and run. Main thing is remove the NavigationView in your SwiftUI Code.
Below is example of the hybrid navigation control with the hosting controller:
import Foundation
import UIKit
import SwiftUI
class MfaChooseMethodsViewController : UIViewController {
var vm : MfaViewModel = MfaViewModel()
func setNavBarAppearance()
{
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
navigationBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationBarAppearance.backgroundColor = .systemBlue
navigationController?.navigationBar.tintColor = .white
navigationItem.largeTitleDisplayMode = .always
let label = UILabel()
label.font = label.font.withSize(30)
label.text = "Two Step Verification"
label.textColor = .white
label.textAlignment = .left
self.navigationItem.titleView = label
label.translatesAutoresizingMaskIntoConstraints = false
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: label.superview, attribute: .centerX, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: label.superview, attribute: .width, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: label.superview, attribute: .centerY, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: label.superview, attribute: .height, multiplier: 1, constant: 0))
//let play = UIBarButtonItem(title: "Play", style: .plain, target: self, action: #selector(playTapped))
//navigationItem.rightBarButtonItems = [add, play]
navigationItem.standardAppearance = navigationBarAppearance
navigationItem.compactAppearance = navigationBarAppearance
navigationItem.scrollEdgeAppearance = navigationBarAppearance
}
override func viewDidLoad() {
super.viewDidLoad()
print("*** data for MfaChooseMethodsViewController:viewDidLoad \(vm)")
setNavBarAppearance()
// 1.
let mfaChooseUIView = MfaMethodsView(model: vm)
// 2.
let hostingController = UIHostingController.init(rootView: mfaChooseUIView)
// 3.
self.addChild(hostingController)
// 4.
hostingController.didMove(toParent: self)
// 5.
self.view.addSubview(hostingController.view)
// 6.
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
hostingController.view.topAnchor.constraint(equalTo: self.view.topAnchor),
hostingController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
])
}
override func viewWillAppear(_ animated: Bool) {
print("*** data for MfaChooseMethodsViewController:viewWillAppear \(vm)")
}
override func viewWillDisappear(_ animated: Bool) {
print("*** data for MfaChooseMethodsViewController:viewWillDisappear \(vm)")
}
}