用Swift编程隐藏TabBarViewController中的选项卡



我有一个TabBarViewController,我想根据某些条件自动使某些选项卡可见/不可见。我尝试了以下操作,但没有成功。self.tabBarController?.tabBar.items?返回零。

class MainPageTabBar: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
if Auth.auth().currentUser != nil {
if let tabBarItem = self.tabBarController?.tabBar.items?[3] {
tabBarItem.isEnabled = false
}
} else {
if let tabBarItem = self.tabBarController?.tabBar.items?[4] {
print("I have tabbb")
tabBarItem.isEnabled = false
}
}
}
}

在您的选项卡vc上执行所需的所有操作

override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.delegate = self
if userStatus == 1 {      //userStatus is sting where check the user status so if userstatus is 1 then tabbar show 4 tabs otherwise tabbar show 3 tabs
self.viewControllers![0].title = "DASHBOARD"
self.viewControllers![1].title = "COMMUNITIES"
self.viewControllers![2].title = "ADMIN ACCOUNTS"
self.viewControllers![3].title = "REPORTS"
}else{
var viewControllers = self.viewControllers
viewControllers?.remove(at: 2)  // here i'm removing my no 2 tabs which is ADMIN ACCOUNTS you can see in above conditions and then i'm replacing that 2 index with third index as you see
self.viewControllers = viewControllers
self.viewControllers![0].title = "DASHBOARD"
self.viewControllers![1].title = "COMMUNITIES"
self.viewControllers![2].title = "REPORTS"
}
}

这段代码还有一件事是针对tabbar类的。

我正在开发xcode 11.1,所以它对我来说很好,希望它对你有用。谢谢

将窗口的rootViewController设置为您想要的视图控制器,如下所示:UIApplication.shared.keyWindow?.rootViewController = vc

最新更新