Encountering a crash in your SwiftUI application when using UIHostingController inside a UINavigationController, specifically when popping from the navigation stack, don’t worry. There’s a workaround to address this issue and prevent the crash.
The crash occurs because the UIHostingController and UINavigationController have conflicts during the deallocation process. To resolve this, you can use a different approach by assigning the UIHostingController’s view to a separate UIViewController. Here’s an example of how you can implement this workaround:
swiftCopy code
let viewController = UIViewController()
viewController.view.addSubview(hostingController.view)
hostingController.view.pinToEdges()
In this workaround, you create a new UIViewController and add the UIHostingController’s view as a subview. By doing this, you avoid the conflict that causes the crash.
Although this workaround resolves the crash, it’s important to note that it doesn’t address the core issue causing the conflict between UIHostingController and UINavigationController. The crash itself may be due to a bug in the specific iOS versions mentioned (16.0 – 16.2). It’s recommended to keep an eye on future iOS updates and check if the issue gets resolved by Apple.
If you’re looking for an alternative solution or seeking further insights on handling this situation in a better way, it would be helpful to consult the Apple Developer Forums or other developer communities to see if others have encountered similar issues and found alternative approaches or workarounds.
Always ensure that you’re running the latest iOS version and keep your development environment updated to benefit from bug fixes and improvements introduced by Apple.