zkn
zkn

Reputation: 79

present view controller on selecting tabbar item

how to present view controller when tabbar item clicked. I just want to display as a popover . Using storyboard

i found that solution but this is not working

  AddImage *yourViewController= (AddImage*)  [self.tabBarController.viewControllers objectAtIndex:3];

    CGFloat tabBarHeight = self.tabBarController.tabBar.bounds.size.height;
    CGRect rect = CGRectMake(0, 0, tabBarHeight, tabBarHeight);
    [AddImage presentPopoverFromRect:rect
                                                                             inView:self.tabBarController.tabBar
                                                           permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

Upvotes: 3

Views: 3130

Answers (2)

Franck Ndame
Franck Ndame

Reputation: 61

Swift 5

Create a class that inherits from UITabBarController and set the protocol UITabBarControllerDelegate.

class CustomTabController: UITabBarController, UITabBarControllerDelegate {

   override func viewDidLoad() {
       super.viewDidLoad()
       self.delegate = self 
   }
}

Implement the method tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool.

From there, a simple if-else statement to check what the current controller class is will suffice.

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController is YourViewController {
        self.present(YourViewController(), animated: true)
        return false
    } else {
        return true
    }
}

Upvotes: 1

Agent Chocks.
Agent Chocks.

Reputation: 1312

For this purpose
1) set the delegate of the tabbar

2) implement the method : -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

3) now display popover for the specific item

Upvotes: 2

Related Questions