Search code examples
iosswiftuikituitabbaruitabbaritem

Swift UITabBarItem as a UIButton


I tried to implement a UITabBar and now I am not able to retreive a callback from a method when an item was selected. Is there a possibillity to just create a @IBAction func therefore? Or do I need to do something else?


Solution

  • In order to implement UITabBar button, your view controller should conform to UITabBarDelegate protocol. You need to implement:

    Swift:

    func tabBar(_ tabBar: UITabBar, didSelectItem item: UITabBarItem!)
    

    Objective-c:

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
    

    You also need to set the delegate (tabBar.delegate = self) in the viewDidLoad of your ViewController

    Edit: Swift 3 answer:

    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    
    }