0

Ive been searching and i cant seem to find out how to disable all tab bar items EXCEPT the home button from being used until a user logs in or creates and account

5 Answers5

3

You can do something like below, create a custom class(TabBarController), extend it from UITabBarController, and write code inside TabBarController class.

Assign TabBarController class to your UITabBarController

extension TabBarController: UITabBarControllerDelegate{
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    // allow your desired controller to be tapped
    if tabBarController.selectedIndex == indexOfHomeControllerInTabBar {
        return true
    }
    return false
}
}

Note: Apple doesn't recommend blocking tabbars, for more info check this link https://developer.apple.com/design/human-interface-guidelines/ios/bars/tab-bars/

Vikky
  • 914
  • 1
  • 6
  • 16
0

here is my approach

  • note that it is kinda a hack way so you might modify as per your needs

  • you will conform to UITabBarControllerDelegate and make your VC the delegate for it in the viewDidLoad

  • then in the "didSelect viewController" delegate method callback you will do your logic and override the selected index as below code

class ViewController: UIViewController, UITabBarControllerDelegate {

    // MARK: Lifecycle Methods
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarController?.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if /* your logged in logic */ {
            self.tabBarController?.selectedIndex = 0 /* assuming that the home is at index 0 */
        }
    }


}
  • note if you did this step in base VC it will be much better and code saving for you
karem_gohar
  • 336
  • 1
  • 3
  • 11
0

You can loop for all items in your Tabbar and disable items you want

        for i in 0..<tabbarController.tabBar.items!.count {
        let item = tabbarController.tabBar.items![i]
        item.isEnabled = i == indexOfHomeTab
    }
Alexandr Kolesnik
  • 1,929
  • 1
  • 17
  • 30
0

Put this somewhere in viewDidLoad()

if let viewControllers = self.tabBarController?.viewControllers {

        for viewController in viewControllers {

            if viewController != viewControllers[0] { // assuming your homeViewController index is 0

            tabBarController?.tabBarItem.isEnabled = false

            }

        }
    }
Celeste
  • 1,519
  • 6
  • 19
0

The short answer is probably "Don't do that." Tabs in a tab bar are supposed to let the user switch between always-available screen at the top level of your UI. If you read Apple's HIG I suspect you will find that what you are trying to do is not recommended.

Better to have each of the other screens show some sort of disabled/inactive state.

Duncan C
  • 128,072
  • 22
  • 173
  • 272