我有一个 UITabBarController
,我有 3 个 View Controller ,用于 3 个标签栏项目,使用关系 segues 连接。
但我想根据用户类型显示 2 或 3 个标签栏项目。例如,对于用户 A 只显示 2 个 UITabBarItem
,对于另一个用户 B 显示所有 3 个 UITabBarItem
。
我怎样才能做到这一点?
最佳答案
在您的自定义选项卡栏实现中尝试以下方法(您必须将其设置为 Storyboard 中的选项卡栏 Controller 的类):
class CustomTabBarController: UITabBarController {
override func awakeFromNib() {
super.awakeFromNib()
// I have no idea how you determine your user type, so fix it according to yourself
if user.type = "A" {
self.viewControllers = [storyboard!.instantiateViewController(withIdentifier: "ViewController1"),
storyboard!.instantiateViewController(withIdentifier: "ViewController2")]
} else {
self.viewControllers = [storyboard!.instantiateViewController(withIdentifier: "ViewController1"),
storyboard!.instantiateViewController(withIdentifier: "ViewController2"),
storyboard!.instantiateViewController(withIdentifier: "ViewController3")]
}
}
}
关于ios - 控制 UITabBar 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48642549/