控制 UITabBar 项目
Controlling UITabBar items
我有一个 UITabBarController
,我有 3 个视图控制器,用于 3 个使用关系转场连接的选项卡栏项目。
但我想根据用户类型显示 2 或 3 个标签栏项目。例如,对于用户 A 只显示 2 UITabBarItem
而对于另一个用户 B 显示全部 3 UITabBarItem
.
我怎样才能做到这一点?
在您的自定义标签栏实现中尝试以下方法(您必须将其设置为故事板中标签栏控制器的 class):
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")]
}
}
}
可能的解决方案:-
- 以编程方式添加 UITabBarItems
- 当只需要 2 个 UITabBarItems 时,以编程方式删除一个 UITabBarItem
- 需要时添加第三个 UITabBarItem
以编程方式添加 UITabBarItems :-
UITabBarItem * itemNew = [[UITabBarItem alloc] initWithTitle:@"Page 1"
image:[UIImage imageNamed:@"page1_image_normal"]
selectedImage:[UIImage imageNamed:@"page1_image_selected"]];
获取现有的 tabBarItems
NSMutableArray *tbItems = [NSMutableArray arrayWithArray:[self.tabBar items]];
//Add your new tabBarItem
[tbItems addObject:itemNew];
//Set your tabBar items to the new array
[self.tabBar setItems:tbItems];
我有一个 UITabBarController
,我有 3 个视图控制器,用于 3 个使用关系转场连接的选项卡栏项目。
但我想根据用户类型显示 2 或 3 个标签栏项目。例如,对于用户 A 只显示 2 UITabBarItem
而对于另一个用户 B 显示全部 3 UITabBarItem
.
我怎样才能做到这一点?
在您的自定义标签栏实现中尝试以下方法(您必须将其设置为故事板中标签栏控制器的 class):
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")]
}
}
}
可能的解决方案:-
- 以编程方式添加 UITabBarItems
- 当只需要 2 个 UITabBarItems 时,以编程方式删除一个 UITabBarItem
- 需要时添加第三个 UITabBarItem
以编程方式添加 UITabBarItems :-
UITabBarItem * itemNew = [[UITabBarItem alloc] initWithTitle:@"Page 1"
image:[UIImage imageNamed:@"page1_image_normal"]
selectedImage:[UIImage imageNamed:@"page1_image_selected"]];
获取现有的 tabBarItems
NSMutableArray *tbItems = [NSMutableArray arrayWithArray:[self.tabBar items]];
//Add your new tabBarItem
[tbItems addObject:itemNew];
//Set your tabBar items to the new array
[self.tabBar setItems:tbItems];