Search code examples
iosswiftuitabbaritem

Two tab bar items with one view controller


I really need your help. I know that what my question contains not so good UI practices, but I need to do it...

In my app I have TabBarController with four items. The first item is "Channel" item, the third item is "Search" item. So, when users selecta "Search" item, I need show him "Channel" and searching something in ChannelViewController. I want that "Channel" and "Search" items have only one view controller - ChannelViewController.

I can present ChannelViewController, but then TabBar navigation isn't showing. I've written this code in SearchViewController:

    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {

        let channelViewController = self.storyboard!.instantiateViewControllerWithIdentifier("channelViewController") as! ChannelViewController
        self.presentViewController(channelViewController, animated: false, completion: nil)

    }

Can I do this without SearchViewController? Can I just add or show/hide when I select "Search" item?

Is it possible do something like this:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {     
    if tabBar.items?.indexOf(item) == 2 {
        // and here some code to show tabBar.items with index 0
    }
}

? Any help is greatly appreciated!


Solution

  • Try this in one of your controllers:

    self.tabBarController?.selectedIndex = 0