如何隐藏标签栏控制器swift中的视图控制器?

时间:2018-02-24 18:24:16

标签: ios swift view controller tabs

如何隐藏标签栏中的某些视图控制器?

例如,我的标签栏有3个视图控制器,但我的应用程序有两种类型的用户:

- user 1 can only access one of those view controllers
- user 2 can access the 3 view controllers

如何知道谁是用户?隐藏某些视图控制器?

我有这段代码

if user1 == 'admin'
  {
    let tabone = EstadisticasViewController()
    let tab1 = UITabBarItem(title: "Estadisticas", image: nil, selectedImage: nil)

    tabone.tabBarItem = tab1
    self.viewControllers = [tabone]
  }

但结果我有一个黑色的视图,在我的故事板视图中,“estadísticas”有按钮,图像和文字。

2 个答案:

答案 0 :(得分:0)

我认为这很容易 首先,您需要添加所有tabbar项, 然后根据你的用户角色隐藏tabbar索引..

if user1 == 'admin'
  {
  //Show All Tabs
}
else
{
  let indexToRemove = 3
   if let tabBarController = self.tabBarController {

    if indexToRemove < tabBarController.viewControllers?.count {
        var viewControllers = tabBarController.viewControllers
        viewControllers?.remove(at: indexToRemove)
        tabBarController.viewControllers = viewControllers
    }
  }
}

答案 1 :(得分:0)

根据您的用户类型设置视图控制器是正确的,您有一个黑色视图,因为您必须从故事板中初始化您的UIViewController:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabOneVC = storyboard.instantiateViewController(withIdentifier: "Your identifier") as! EstadisticasViewController