0

I'm new to SwiftUI, but experienced in UIKit.
Recently I started new app and Created first LoginView using UIKit but then after created SignUp and VerifyOTP view using SwiftUI.
LoginViewController was embeded in UINavigationController and that's why I pushed SignUpView using UIHostingViewController.
But then I need to navigate to VerifyOTP view. I used NavigationStack / NavigationView but it creates another NavigationBar under main. So I have 2 navigationBars on SignUp and VerifyOTP view.
Is there something I can use UINavigationController even for SwiftUI navigation?

Mrugesh Tank
  • 3,495
  • 2
  • 29
  • 59
  • NavigationStack would create another instance and will not point to the existing UINavigationController. For a common stack you would have to keep one - either UINavigationController or NavigationStack. – singhabhi13 Dec 18 '22 at 09:42
  • @singhabhi13 Yeah, That I understood. but how to solve the issue? I want to keep UINavigationController. But I also want navigation in SwiftUI views – Mrugesh Tank Dec 28 '22 at 10:35
  • Could you share the sample code or project? – singhabhi13 Dec 31 '22 at 06:19
  • It was pretty simple, Create an app with UIKit, Embed the initial ViewController in UINavigationController, Create SwiftUI push it with hostingViewController – Mrugesh Tank Jan 20 '23 at 09:55

1 Answers1

0

you can push the controller like

ex: swiftUI class -> struct SplashView: View {

 func PushController() {
    self.setNavigation()
    
    let contentView = SplashView()
    self.setRootController(for: UIHostingController(rootView: contentView))
}

private func setNavigation() {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .clear// UIColorManager().getAppDefaultOrange()
    appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    appearance.shadowColor = .clear
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
    UINavigationBar.appearance().tintColor = .black
}

private func setRootController(for rootController: UIViewController) {
    if let window = UIApplication.shared.windows.first {
        // Set the new rootViewController of the window.
        if let root = window.rootViewController {
            window.rootViewController?.dismiss(animated: true, completion: {
                window.rootViewController = UINavigationController(rootViewController: rootController)
                window.makeKeyAndVisible()
             })
        } else {
            let navigationController = UINavigationController(rootViewController: rootController)
            
            navigationController.navigationBar.isHidden = true
            window.rootViewController = rootController
            window.makeKeyAndVisible()
        }
        
        // A mask of options indicating how you want to perform the animations.
        let options: UIView.AnimationOptions = .transitionCrossDissolve

        // The duration of the transition animation, measured in seconds.
        let duration: TimeInterval = 0.3

        // Creates a transition animation.
        // Though `animations` is optional, the documentation tells us that it must not be nil. ¯\_(ツ)_/¯
        UIView.transition(with: window, duration: duration, options: options, animations: {}, completion: { completed in
            // Do something on completion here
        })
    }
}

just need to call PushController().

Sagar Bhut
  • 657
  • 6
  • 28