簡體   English   中英

如何在沒有 StoryBoards 的情況下以編程方式從 viewcontroller 轉換到 TabbarController?

[英]How to transition from viewcontroller to TabbarController Programmatically without StoryBoards?

從我的入職視圖控制器中,我需要轉換到我的 tabBarController,它也是一個導航控制器,之后我想將其作為根視圖控制器。

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

線程 1:“應用程序試圖以模態方式呈現視圖 controller <MyStarterProject.TabBarController: 0x7f9bd8011400> 具有父視圖 controller <UINavigationController: 0x7f9bd70>

我喜歡像模態演示或交叉溶解一樣轉換它。 不只是突然出現一個rootviewcontroller。

這就是我想要做的,這解決了我的問題。

   @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
        })
    }

改變這個:

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

有了這個:

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

請試試這個。

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

如果您需要更多詳細信息。 請訪問此博客https://fluffy.es/how-to-transition-from-login-screen-to-tab-bar-controller/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM