Search code examples
swiftuitabbarcontrollergesture

Swift: Gesturing doesn't bring back the TabBar


I am trying to move back from a ViewController (simple text page) to a main ViewController with a TabBar at the bottom using gesturing.

The gesture works as I return to the original main screen but there is no TabBar.

In the simple ViewController I use this code to go back to the originating ViewController.

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "AboutViewControllerID")
            self.present(controller, animated: true, completion: nil)
            if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AboutViewControllerID") as? AboutViewController
            {
                present(vc, animated: true, completion: nil)
            }
        default:
            break
        }
    }
}

In the ViewController with the TabBarController I have tried the following lines to rejuvinate the TabBarController but without any joy.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    tabBarController?.tabBar.isHidden = false
     NSLog("TabBar true")
}

Any ideas?


Solution

  • First if you want to go-back don't use present as it will add the same VCS twice in the stack , you have to use unwindSegue/dismiss , or load the tabBar itself with id

    self.dismiss(animated: true) {
    
         // use this if it's not directly behind the dismissed VC
         let tab = storyboard.instantiateViewController(withIdentifier: "tabBarID") as! UITabBarController
         UIApplication.shared.keyWindow?.rootViewController = tab
    
    }
    

    // enter image description here