1)保存 UIViewController 的数组
@property(nullable,nonatomic,copy)NSArray<__kindofUIViewController *> *viewControllers;
2)一次性设置保存 UIViewController 的数组,并设置动画效果
- (void)setViewControllers:(NSArray<__kindofUIViewController *> * __nullable)viewControllers animated:(BOOL)animated;
假如在此之前已经创建好了 6 个 UIViewController 对象,并且都是用了系统的 UITabBarItem 对象,因为本节的重点不在这,所以就省略了
代码如下
// 创建 UITabBarController 对象
UITabBarController * tabBarController = [[UITabBarController alloc] init];
没有动画显示
tabBarController.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC, fifthVC, sixthVC];
使用动画显示
[tabBarController setViewControllers:@[firstVC, secondVC, thirdVC, fourthVC, fifthVC, sixthVC] animated:YES];
虽然结果和上面的图一致,但是在显示 工具栏的上的 UITabBarItem 对象是有一个动画的
2. 选中的 UIViewController
1)设置选中的 UITabBarItem 的索引
@property(nonatomic)NSUInteger selectedIndex;
2)设置选中的视图控制器
@property(nullable,nonatomic,assign)__kindofUIViewController *selectedViewController;
因为 UITabBarController 对象在启动时会默认显示第一个 UIViewController 对象,即索引为 0 的 UIViewController
设置启动时显示下表为 3 的 UIViewController,即第 4 个 UIViewController 对象
tabBarController.selectedIndex = 3;
tabBarController.selectedViewController = fourthVC;
如图,启动时就已经显示在第 4 个 UIViewController
3. ”更多“
当 UITabBarController 对象显示的 UIViewController 对象超过 5 个时,系统会自动创建一个 UINavigationController 对象,并将在原本显示第 5 个 UITabBarItem 的地方显示一个 “more” 的 UITabBarItem 对象,并且该 “more” 对应的 UIViewController 是一个 UINavigationController 对象,然后再把第 5 个以后的 UIViewController 都加入到这个 UINavigationController 对象的栈中
1)返回 “more” 对应的 UINavigationController 对象
@property(nonatomic,readonly)UINavigationController *moreNavigationController;
2)保存 “更多” 中的视图控制器对象
@property(nullable,nonatomic,copy)NSArray<__kindofUIViewController *> *customizableViewControllers;
6. 获取 标签栏 对象
就是 UITabBarController 对象下方的工具条,一个 UIViewController 对象在 tabBar 中对应一个 UITabBarItem 对象
@property(nonatomic,readonly)UITabBar *tabBar;
8. 代理对象
@property(nullable,nonatomic,weak)id<UITabBarControllerDelegate> delegate;
三、UITabBarControllerDelegate
1. 点击 UITabBarItem 时调用,控制哪些 UIViewController 能被点击
返回 YES : 能被点击;返回 NO 时,不能被点击(也就是说不能调用 2 的方法)
viewController : 发送该消息的接受者将 viewControllers 属性传递给 viewController
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
2. 当选中某一个 UIViewController 时调用该方法(即在 UITabBar 中选中某个 UITabBarItem)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
3. 开始 编辑 之前时调用
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindofUIViewController *> *)viewControllers;
4. 结束 编辑 之前时调用
changed : 若是改变了视图控制器的顺序,则返回 YES;否则返回 NO
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindofUIViewController *> *)viewControllers changed:(BOOL)changed;
5. 已经结束编辑时调用
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindofUIViewController *> *)viewControllers changed:(BOOL)changed;
6. 监控屏幕切换;返回 屏幕切换的枚举
- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController;
7. 屏幕首选的方向
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController;
四、分类 UIViewController (UITabBarControllerItem)
1. 当前 UIViewController 的 UITabBarItem,如果要设置当前 UIViewController 的 UITabBarItem 的标题等,就要先获取该 UIViewController 的 tabBarItem 对象
@property(null_resettable,nonatomic,strong)UITabBarItem *tabBarItem;
2. 获取当前 UIViewController 的 UITabBarController 对象,如果有多个 UIViewController 对象都在同一个 UITabBarController 中,那么返回的是同一个 UITabBarController 镀对象
@property(nullable,nonatomic,readonly,strong)UITabBarController *tabBarController;