Use SwiftUI View with UIKit Project easily.
With UIHostingController, I faced bugs that were hard to predict and could not be resolved. That's why I made SwiftUINavigator to solve those problems.
let viewController = SwiftUIView() // View
.asViewController() // WrapperViewController
.title("SwiftUIView") // UIViewController
.backgroundColor(.gray) // UIViewController
.hidesBottomBarWhenPushed(true) // UIViewController
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: scene.coordinateSpace.bounds)
window?.windowScene = scene
let controller = RootView().asViewController()
let navigationController = UINavigationController(rootViewController: controller)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
}
struct RootView: Wrappable {
weak var navigator: Navigator?
var body: some View {
Button("Button") {
let controller = DestinationView().asViewController()
navigator?.push(controller)
}
}
}
struct DestinationView: Wrappable {
weak var navigator: Navigator?
var body: some View {
Button("Button") {
navigator?.pop()
}
}
}