amirtmgr
amirtmgr

Reputation: 303

How to disable all UITabBarItems when a ViewController is presented modally?

I've implemented UITabBar to my app. Inside ViewController of one UITabBar item, I've shown another ViewController which is presented modally. At this stage, I want to disable all the UITabBar item and re-enable in willdisappear.

Inside the following delegate, if I get the presented modally ViewController then, on comparing I can return without any action. But I'm confused on how to get visible ViewController which is presented modally. Will this approach works?

 (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//HOW CAN I GET HERE VISIBLE VIEWCONTROLLER I.E. PRESENTED MODALLY VIEWCONTROLLER.
}

Upvotes: 0

Views: 59

Answers (2)

Ankit Jayaswal
Ankit Jayaswal

Reputation: 5669

Rather in disabling the tab bar, you can hide tab bar in viewWillAppear of presented view controller and again show it in viewWillDisappear.

In viewWillAppear:

    tabBarController?.tabBar.isHidden = true

In viewWillDisappear:

    tabBarController?.tabBar.isHidden = false

But if your motive is get visible view controller, you can use the following UIApplication extension.

extension UIApplication {

    class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(base: selected)
            }
        }
        if let presented = base?.presentedViewController {
            return topViewController(base: presented)
        }
        return base
    }
}

Upvotes: 0

Chirag Kothiya
Chirag Kothiya

Reputation: 1099

Disable :

self.tabBarController.tabBar.userInteractionEnabled = NO;

Enable

self.tabBarController.tabBar.userInteractionEnabled = YES;

Upvotes: 1

Related Questions