I successfully added a UITabBar to my UIViewController. But, I'm unable to switch view controllers when tabs are tabbed. I used the following approaches :
public func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
case 1 :
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "HardwareViewController") as! HardwareViewController
self.addChildViewController(viewController)
break
case 2 :
self.performSegue(withIdentifier : IDENTIFIER_SEGUE_SYSTEMVC, sender : nil)
break
case 3 :
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "SystemViewController") as! SystemViewController
self.view.insertSubview(viewController.view!, belowSubview: self.tabBar)
break
case 4 :
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "HardwareViewController") as! HardwareViewController
self.view.insertSubview(viewController.view!, belowSubview: self.tabBar)
break
case 5 :
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier : "SystemViewController") as! SystemViewController
self.view.insertSubview(viewController.view!, belowSubview: self.tabBar)
break
default:
break
}
}
But, none of these worked. Segue presents a new ViewController and tabbar hides.It takes the control all together to a new viewcontroller. I have 2 doubts :
- How to show respective viewcontrollers in the same view when respective tab bars are tabbed?
- How to load view controller with tabbar with an initial view controller being shown in it? Lets say the view controller to be displayed on first tab should also be shown when view controller with tab bar is loaded.