简体   繁体   中英

How to transition from viewcontroller to TabbarController Programmatically without StoryBoards?

From my onboarding viewcontroller i need to transition to my tabBarController which is also a navigationController and i want to make it as a root viewcontroller afterwards.

 @objc func willGoToMain(sender: UIButton!) {
        let tabBarController = TabBarController()
        let navigationController = UINavigationController(rootViewController: tabBarController)
        navigationController.isNavigationBarHidden = true
        self.present(tabBarController, animated: true, completion: nil)
 }

Thread 1: "Application tried to present modally a view controller <MyStarterProject.TabBarController: 0x7f9bd8011400> that has a parent view controller <UINavigationController: 0x7f9bd7022800>

I like to transition it like a modal presentation or cross dissolve. not just to appeared as a rootviewcontroller all of a sudden.

Here's what i'm trying to do and this solves my problem.

   @objc func willGoToMain(sender: UIButton!) {
        
        guard let window = UIApplication.shared.keyWindow else {
            return
        }
        let tabbarController = TabBarController()
        let navigationController = UINavigationController(rootViewController: tabbarController)
        navigationController.isNavigationBarHidden = true
                        
        window.rootViewController = navigationController
        window.makeKeyAndVisible()
        
        let options: UIView.AnimationOptions = .transitionCrossDissolve
        let duration: TimeInterval = 0.3
        UIView.transition(with: window, duration: duration, options: options, animations: {}, completion:
        { completed in
            // maybe do something on completion here
        })
    }

Change this:

self.present(tabBarController, animated: true, completion: nil)

with this:

self.present(navigationController, animated: true, completion: nil)

Please try this one.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = storyboard.instantiateViewController(identifier: "MainTabBarController")
mainTabBarController.modalPresentationStyle = .fullScreen        
self.present(mainTabBarController, animated: true, completion: nil)

If you need more details. please visit this blog https://fluffy.es/how-to-transition-from-login-screen-to-tab-bar-controller/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM