ios - 如何在标签栏 Controller 中隐藏 View Controller swift?

标签 ios swift view controller tabs

如何隐藏标签栏中的某些 View Controller ?

例如,我的选项卡栏有 3 个 View Controller ,但我的应用程序有两种类型的用户:

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

如何隐藏某些 View Controller ,知道用户是谁?

我有这个代码

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

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

但结果我有一个黑色的 View ,在我的 Storyboard View “estadísticas”中有按钮、图像和文本。

最佳答案

我认为这很容易 首先您需要添加所有选项卡栏项目, 然后根据您的用户角色隐藏标签栏索引,如下所示..

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
    }
  }
}

关于ios - 如何在标签栏 Controller 中隐藏 View Controller swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48966195/

相关文章:

performance - 在 View 中封装 Postgres 查询使其非常慢

ios - Swift 版本 NativeScript

ios - 如何根据确认对话框防止SegmentedControl索引更改?

php - 仅显示前 5 个结果

swift - 快速从不同文件加载变量

ios - 使用 CAShapeLayer 多次动画循环进度

android - 如何通过 android 命名空间的 ID 访问 View

iphone - 如何从 iPhone 模拟器录制电影?

ios - AFNetworking 2.0 - 是否使用子类?

ios - Testflight 假 iOS 版本检测