How to remove programmatically a tab bar item created in parent class NIB file?

感情迁移 提交于 2019-11-28 11:03:26

The following code has the solution:

NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
[tbViewControllers removeObjectAtIndex:2];
[self.tabBarController setViewControllers:tbViewControllers];
mourodrigo

Swift 4

func removeTab(at index: Int) {
    guard let viewControllers = self.tabBarController?.viewControllers as? NSMutableArray else { return }
    viewControllers.removeObject(at: index)
    self.tabBarController?.viewControllers = (viewControllers as! [UIViewController])
}

This is what it works for me for Swift 4

  1. Create a custom UITabBarController class.
  2. Assign the custom UITabBarController class to the view on storyboard.
  3. Remove the UIViewController on viewDidLoad:

    class TabViewController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.viewControllers?.remove(at: 1)
        }
    }
    

You can hold a reference to that tab bar object in your class and perform the desired actions on it.

IBOutlet <Type> name;

Connect it via Interface builder and you can perform actions, and in your case you may be thinking of removing it from superview.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!