如何隐藏 UITabbarIcon?

How can I hide a UITabbarIcon?

我目前正在开发一个 iOS-App,它将有 5 个标签栏图标。其中之一只有在您被允许看到的情况下才应该可见。 我怎样才能隐藏这个图标?

使用以下代码,您可以从标签栏中删除指定的标签:

let tabIndex = 3
if let tabBarController = self.tabBarController {
    if tabIndex < tabBarController.viewControllers?.count {
        var allViewControllers = tabBarController.viewControllers
        allViewControllers?.remove(at: tabIndex)
        tabBarController.viewControllers = allViewControllers
    }
}

如果要从 tabBar 中删除 UIViewController,请在 UITabBarControllerClass

中添加这行代码

删除

self.viewControllers?.remove(at: tabIndex) // replace the tabIndex which you want to remove

为添加

self.viewControllers?.insert(viewController, at: tabIndex) // replace the viewcontroller with your controller and tabIndex with your index