errata
errata

Reputation: 6031

Hiding UITabBarController's tabBarItem while being able to show associated view nevertheless

I am trying to create a navigation for my iPhone "Tabbed App" which would consist of (obviously) UITabBarController and SWRevealViewController for revealing side-menus.

All the views in my application must have both UITabBarController and UINavigationBar displayed, however, links which appear in left-side menu (handled by SWRevealViewController) must not appear in UITabBarController.

My left-side menu links are handled in this way:

import UIKit

class MenuTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.clearsSelectionOnViewWillAppear = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedIndex = (indexPath as NSIndexPath).row + 1  // hardcoded for time being
        let tabBarController = revealViewController().frontViewController as! UITabBarController

        let navController = tabBarController.viewControllers![selectedIndex] as! UINavigationController
        navController.popToRootViewController(animated: true)

        tabBarController.selectedIndex = selectedIndex
        revealViewController().pushFrontViewController(tabBarController, animated: false)
    }
}

Now, I tried to remove a link for one of the views which I don't want to show in my UITabBarController as follows:

import UIKit

class TabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let index = 2  // hardcoded for time being
        viewControllers?.remove(at: index)
    }
}

but if I click associated link in left-side menu now, I get an NSRangeException index 2 beyond bounds [0 .. 1] error (of course, because I removed the particular tabBarItem from UITabBarController).

My question is: how can I "hide" the item from UITabBarController but still being able to reference it (and open it) from my side menu?

UPDATE
My storyboard at the moment looks like this:enter image description here

Upvotes: 0

Views: 144

Answers (1)

DonMag
DonMag

Reputation: 77462

It's probably not a good idea to use a "menu" to manipulate the Tabs - that's what Apple has designed the More... and Edit... features for.

Depending on your overall design / navigation / user experience flow, two reasonable options would be:

  1. Instead of replacing the current selected Tab, .present a modal view controller, with a "Cancel" or "Save" or "Done" button to .dismiss it (whatever would be appropriate).

  2. Since you state each Tab's ViewController is a NavigationController, you could .push the menu-selected view controller onto the current stack. Then your interface could use the standard "< Back" button navigation.

Good Luck :)

Upvotes: 1

Related Questions