Search code examples
iosswift3uitabbarcontrollerswrevealviewcontroller

SWRevealViewController with UITabBarController


app configuration

This is My configuration with SWRevealViewController, UITabController, four ViewControllers connected to the UITabBarController via UINavgationController. The tab bar controller is set as sw_front and the left top table view is set as the sw_rear. The code I used for the sw_rear is

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabBar") as! MainTabBarController

    if(indexPath.row == 0){
        let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "superDeals") as! SuperDealsViewController
        let navigationController = UINavigationController(rootViewController: destinationVC)
        navigationController.setViewControllers([destinationVC], animated: true)
        tabBarController.setViewControllers([navigationController], animated: true)
        tabBarController.selectedIndex = 0
        self.revealViewController().setFront(tabBarController, animated: true)
        self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
    }else if(indexPath.row == 1){
        let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "allDeals") as! AllDealsViewController
        let navigationController = UINavigationController(rootViewController: destinationVC)
        navigationController.setViewControllers([destinationVC], animated: true)
        tabBarController.setViewControllers([navigationController], animated: true)
        tabBarController.selectedIndex = 1
        self.revealViewController().setFront(tabBarController, animated: true)
        self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
    }else if(indexPath.row == 2){
        let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "coupon") as! CouponsViewController
        let navigationController = UINavigationController(rootViewController: destinationVC)
        navigationController.setViewControllers([destinationVC], animated: true)
        tabBarController.setViewControllers([navigationController], animated: true)
        tabBarController.selectedIndex = 2
        self.revealViewController().setFront(tabBarController, animated: true)
        self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
    }else if(indexPath.row == 3){
        let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "forum") as! ForumViewController
        let navigationController = UINavigationController(rootViewController: destinationVC)
        navigationController.setViewControllers([destinationVC], animated: true)
        tabBarController.setViewControllers([navigationController], animated: true)
        tabBarController.selectedIndex = 3
        self.revealViewController().setFront(tabBarController, animated: true)
        self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
    }else if(indexPath.row == 4){
        let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "stores") as! StoresViewController
        let navigationController = UINavigationController(rootViewController: destinationVC)
        navigationController.setViewControllers([destinationVC], animated: true)
        self.revealViewController().setFront(navigationController, animated: true)
        self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
    }else if(indexPath.row == 5){
        let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "categories") as! categoriesViewController
        let navigationController = UINavigationController(rootViewController: destinationVC)
        navigationController.setViewControllers([destinationVC], animated: true)
        self.revealViewController().setFront(navigationController, animated: true)
        self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
    }else if(indexPath.row == 6){
        if(UserDefaults.standard.object(forKey: "token") == nil){
            let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "login") as! LoginViewController
            let navigationController = UINavigationController(rootViewController: destinationVC)
            navigationController.setViewControllers([destinationVC], animated: true)
            self.revealViewController().setFront(navigationController, animated: true)
            self.revealViewController().bounceBackOnLeftOverdraw = true
            self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
        }else{
            UserDefaults.standard.removeObject(forKey: "token")
            UserDefaults.standard.synchronize()
            let alert = UIAlertController(title: "Log out successful.", message: nil, preferredStyle: .alert)
            self.present(alert, animated: true, completion: nil)

            let when = DispatchTime.now() + 0.5
            DispatchQueue.main.asyncAfter(deadline: when, execute: {
                alert.dismiss(animated: true, completion: {
                    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "base") as! SWRevealViewController
                    self.present(vc, animated: true, completion: nil)
                })
            })
        }
    }
}

The code is working and navigating to the viewcontrollers properly, but the tab bar items are not showing. I have attached the images for clarification. Can someone please help? I want to show the tabbar in each of my viewcontrollers..

How it should be

How it is currently showing


Solution

  • You should only select the index

    let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabBar") as! MainTabBarController
    
    if(indexPath.row == 0){
    
        tabBarController.selectedIndex = 0
        self.revealViewController().setFront(tabBarController, animated: true)
        self.revealViewController().setFrontViewPosition(FrontViewPosition.left, animated: true)
    }