在Swift中删除TabBar项目
我目前尝试find一种方法来删除运行应用程序TabBar项目时,我find了一种方法来启用或禁用它,但不完全删除它。
为了禁用它,我做:
在ViewDidLoad中
if let tabBarItem = self.tabBarController?.tabBar.items?[3] as? UITabBarItem { tabBarItem.enabled = false }
这工作得很好,但用户仍然可以看到TabBar项目,我只是完全删除它,有没有办法?
我想通过Parse触发TabBarItem,如果我把Parse数据设置为true,它应该显示其他方式不应该。
你想设置你的viewControllers
属性的数组,你排除了你不想拥有的特定viewController。
if let tabBarController = self.tabBarController { let indexToRemove = 3 if indexToRemove < tabBarController.viewControllers?.count { var viewControllers = tabBarController.viewControllers viewControllers?.remove(at: indexToRemove) tabBarController.viewControllers = viewControllers } }
对于那些只是想禁用一个项目。 使用@ Daniele解决scheme中的这段代码。 并把它放在你的UITabBarController类中
viewDidLoad() { let index = 0 //0 to 5 viewControllers?.remove(at: index) }
Swift 3.1:只删除Tab Bar Controller中的一个索引(你可以在viewDidLoad和viewDidAppear中使用这个方法)
override func viewDidAppear(_ animated: Bool) { } override func viewDidLoad() { super.viewDidLoad() } viewControllers?.remove(at:0) // for 0 index viewControllers?.remove(at:1) // for 1 index viewControllers?.remove(at:2) // for 2 index
如果您在选项卡栏中有4个索引,并且您想删除最后2个索引
viewControllers?.remove(at:2) viewControllers?.remove(at:2)
第一行将删除索引第三个,你会从4剩下3,再次删除第二个索引时,它将再次删除第三个索引,然后你将保持最后2个索引。
更好的方法是只使用文字而不是图像。 select“空格”作为文本,然后禁用。 那么用户将无法看到它。 我还没有testing,但我确定它会工作。