Keep tab as selected UITabBarController

 

Recently, I came across a situation where I have to  force a current selected tab to remain selected in UITabBarController, when user select or tap a different tab. So i though worthy of sharing the code with all the community. To make this happen, one can use UITabBarController delegate

– (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
 

According to APPLE documentation it states:

Asks the delegate whether the specified view controller should be made active.
 
The tab bar controller calls this method in response to the user tapping a tab bar item. You can use this method to dynamically decide whether a given tab should be made the active tab.
 
– (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if (remainActive )
    {
        return NO;
    }
    
    return YES;
}
 
In above delegate method, we return false if we want our current selected tab to remain active irrespective of user tab selection and no if otherwise.