将UITabBarController设置为rootViewController

时间:2019-01-03 01:28:57

标签: ios swift

我有一个UITabBarController,如果用户会话仍处于活动状态,我想显示此屏幕而不显示登录屏幕。

我的UITabBarController有3个ViewControllers,问题是我没有在底部看到TabBar链接,而且无法导航。

enter image description here

没有以下代码,一切正常。我的意思是登录后可以导航。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        FirebaseApp.configure()

        if Auth.auth().currentUser != nil {
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.makeKeyAndVisible()
            window?.rootViewController = HomeTabBarController()
        }

        return true
    }

我还尝试了以下代码来设置rootViewController,但这是相同的问题。

当我尝试将另一个视图控制器之一设置为root(子级之一)时,选项卡栏根本不显示

var rootView: MyRootViewController = MyRootViewController()

if let window = self.window{
    window.rootViewController = rootView
}

我在这里做什么错了?

enter image description here

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,并且遇到了您的帖子,您的代码存在的问题是HomeTabBarController()正在创建一个全新的TabBarController,因此请尝试使用以下方法修复该问题。

if Auth.auth().currentUser != nil {{

            print("******************************User Present******************************")
            self.window = UIWindow(frame: UIScreen.main.bounds)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)

            // create view controllers from storyboard
            // Interface Builder -> Identitiy Inspector -> Storyboard ID
            // Set up the Tab Bar Controller to have two tabs
            let tabBarController  = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
            // Make the Tab Bar Controller the root view controller
            window?.rootViewController = tabBarController
            window?.makeKeyAndVisible()
        }

编辑确保将标识符添加到TabBarController

    // Interface Builder -> Identitiy Inspector -> Storyboard ID

答案 1 :(得分:1)

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        //// this code changes the initial point of aap///////

        window = UIWindow(frame: UIScreen.main.bounds)
        let nav = UINavigationController()
        let myview = SettingTabbar()
        nav.viewControllers = [myview]
        window?.rootViewController = nav
        window?.makeKeyAndVisible()

        return true
    }
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
    //Setting TabBar
    let tabbar = UITabBarController()

    //Designing Tabbar Item Images
    let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
    let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
    let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)

    //Getting TabBar ViewControllers
    let TableView = newViewController()
    let CollectionView = PersonCollectionViewController()
    let InsertRec = nextViewController()
    //Setting ViewControllers on TabBar Items
    TableView.tabBarItem = table
    CollectionView.tabBarItem = collection
    InsertRec.tabBarItem = insert
    let controllers = [TableView,CollectionView,InsertRec]
    tabbar.viewControllers = controllers
    tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
    //Setting Title
    tabbar.navigationItem.title = "Person Record"

    return tabbar

}

答案 2 :(得分:0)

问题是这一行:

for (jj in seq_along(colGroups)) 
  finalMatrix[ , colGroups[jj]] = 
    finalMatrix[ , colGroups[jj]] & exampleMatrix[ , jj]

那是错误的HomeTabBarController。这是一个没有子代的全新HomeTabBarController。您需要获取情节提要中的HomeTabBarController。

答案 3 :(得分:0)

我终于找到了解决方案:正如@matt所建议的,我必须获取情节提要中的HomeTabBarController。

    window = UIWindow(frame: UIScreen.main.bounds)


   let storyboard = UIStoryboard.init(name: "Main", bundle: nil)

   // controller identifier sets up in storyboard utilities
   // panel (on the right), it called Storyboard ID
   let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController

   self.window?.rootViewController = viewController
   self.window?.makeKeyAndVisible()

   window?.makeKeyAndVisible()
   window?.rootViewController = viewController
相关问题