M1X
M1X

Reputation: 5354

Set UITabBarController as rootViewController

I have a UITabBarController and I want to show this screen and not the login screen if the user session is still active.

My UITabBarController has 3 ViewControllers and the problem is that I don't see the TabBar links in the bottom and I'm unable to navigate.

enter image description here

Without the following code everything works fine. I mean after login I can navigate.

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
    }

I have also tried the following code to set the rootViewController but it's the same problem.

When I try to set one of the other view controllers as root (one of the children), the Tab Bar doesn't show at all

var rootView: MyRootViewController = MyRootViewController()

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

What am I doing wrong here?

enter image description here

Upvotes: 2

Views: 8148

Answers (4)

Ussaid Iqbal
Ussaid Iqbal

Reputation: 796

I was facing same issue and I came across your post, the problem with your code is that HomeTabBarController() is creating a whole new TabBarController so to fix it try the following approach I used.

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

Edit Make sure to add the Identifier to your TabBarController

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

Upvotes: 4

Muhammad Hashim
Muhammad Hashim

Reputation: 3

    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

}

Upvotes: 1

M1X
M1X

Reputation: 5354

I finally found the solution: As @matt suggested I had to fetch the HomeTabBarController that’s in the storyboard.

    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

Upvotes: 0

matt
matt

Reputation: 534949

The problem is this line:

window?.rootViewController = HomeTabBarController()

That is the wrong HomeTabBarController. It is a totally new HomeTabBarController with no children. You need to fetch the HomeTabBarController that’s in the storyboard.

Upvotes: 0

Related Questions