UITabBarController的使用
引入:上一篇博客有提到UINavigationController的使用,这篇博客我先重点介绍这个和它使用一样广泛的UITabBarController,我们比较常见的是微信的下方四个按钮,可以切换不同的页面。每个页面都对应着不同的控制器,在UITabBarController中,管理着若干子控制器,他们之间是并列的关系,在添加到TabBar之后就不会被销毁,只会相互切换,实测过tabBar1和tabBar2的视图会相继调用willMoveToWindow和didMoveToWindow,因此本质是不断地切换在window上的显示权。
下面结合源码直接看官方给出的属性和方法API很简短,我会在重要的地方给出注释。
UITabBarController:
#import <Foundation/Foundation.h>
#import <UIKit/UIViewController.h>
#import <UIKit/UIViewControllerTransitioning.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UITabBar.h>
NS_ASSUME_NONNULL_BEGIN
//申明这里会用到这四个类
@class UIView, UIImage, UINavigationController, UITabBarItem;
//申明了一个协议
@protocol UITabBarControllerDelegate;
//UITabBarController继承于UIViewController,实现UITabBarDelegate和NSCoding协议(NSCoding表面支持归档)
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>
//若干控制器成员
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
//对vc栈整体更新
- (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated;
//当前被选中的控制器
@property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController; // This may return the "More" navigation controller if it exists.
//被选中的控制器的下标
@property(nonatomic) NSUInteger selectedIndex;
//返回更多导航控制器
@property(nonatomic, readonly) UINavigationController *moreNavigationController __TVOS_PROHIBITED; // Returns the "More" navigation controller, creating it if it does not already exist.
//控制器数组
@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers __TVOS_PROHIBITED; // If non-nil, then the "More" view will include an "Edit" button that displays customization UI for the specified controllers. By default, all view controllers are customizable.
//tabBar实例
@property(nonatomic,readonly) UITabBar