跟UINavigationController类似,UITabBarController也是专门用来管理多控制器的,它能够轻松完成控制器之间的切换。
1 UITabBarController的创建
新建一个空项目,通过代码来创建UITabBarController
(注意:如果使用“Single View Application”创建项目的话默认是有Main.storyboard和Main.storyboard关联的控制器的,所以需要将项目的Main Interface去掉内容Main)
实例
下面拿一个实例来说明创建UITabBarController的步骤:
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
// 创建window
self.window = [[UIWindow alloc] initWithFrame:[UIScreenmainScreen].bounds];
// 初始化 UITabBarController
UITabBarController *tab = [[UITabBarController alloc]init];
// 创建子控制器
UIViewController *vc1 = [[UIViewControlleralloc] init];
UIViewController *vc2 = [[UIViewControlleralloc] init];
UIViewController *vc3 = [[UIViewControlleralloc] init];
UIViewController *vc4 = [[UIViewController alloc] init];
// 设置子控制器的颜色
vc1.view.backgroundColor = [UIColorredColor];
vc2.view.backgroundColor = [UIColororangeColor];
vc3.view.backgroundColor = [UIColorblueColor];
vc4.view.backgroundColor = [UIColor yellowColor];
// 通过设置子控制器数组来添加子控制器
tab.viewControllers = @[ vc1, vc2, vc3 ,vc4];
或者通过添加单个子控制器的方式
[tab addChildViewController:vc1];
[tab addChildViewController:vc2];
[tabaddChildViewController:vc3];
[tabaddChildViewController:vc4];
// 设置window的根控制器为 tab
self.window.rootViewController = tab;
// 显示到屏幕上
[self.window makeKeyAndVisible];
//打印UITabBarController的View的Frame;
NSLog(@"%@",tab.view);
//打印UITabBarController的UITabBar的Frame;
NSLog(@"%@",tab.tabBar);
//打印UITabBarController的UITabBar的子视图UITabBarButton的Frame;
NSLog(@"%@",tab.tabBar.subviews);
returnYES;
}
viewControllers和childViewControllers的区别
UITabBarController和UINavigationController控制器都可以通过
viewControllers 或childViewControllers获取它所管理的子控制器,并且获取的子控制器地址一样。
这两个属性的区别:
1> viewControllers是UITabBarController控制器的strong属性;
@interfaceUITabBarController:UIViewController
<UITabBarDelegate,NSCoding>
@property(nonatomic,copy) NSArray*viewControllers;
2> childViewControllers是父类UIViewController的readonly属性。
@interface UIViewController : UIResponder
<NSCoding,UIAppearanceContainer,UITraitEnvironment,UIContentContainer>
@property(nonatomic,readonly) NSArray *childViewControllers
2 UITabBar
如果UITabBarController有N个子控制器,那么UITabBar内部就会有N个UITabBarButton作为子控件。
UITabBar的结构
如果UITabBarController有4个子控制器,那么UITabBar的结构大致如下图所示
UITabBarButton的属性
UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定,
UITabBarItem有以下属性影响着UITabBarButton的内容。
@property(nonatomic,copy) NSString *title; //标题文字
@property(nonatomic,retain) UIImage *image; //标题文字
@property(nonatomic,retain) UIImage*selectedImage; //选中时的图标
@property(nonatomic,copy) NSString *badgeValue; //提醒数字
研究:1知识点中实例的打印结果
打印结果:
opaque不透明的,值为NO的时候表示:是透明的。
<UILayoutContainerView:0x7f88d96253e0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer:0x7f88d9620b10>>
<UITabBar: 0x7f921b425eb0; frame = (0 618; 375 49); autoresize = W+TM;layer = <CALayer: 0x7f921b41c130>>
(
"<UITabBarButton: 0x7f921b4290f0;frame = (2 1; 90 48); opaque = NO; layer = <CALayer:0x7f921b429570>>",
"<UITabBarButton: 0x7f921b429e00;frame = (96 1; 90 48); opaque = NO; layer = <CALayer:0x7f921b429d30>>",
"<UITabBarButton: 0x7f921b42a380;frame = (190 1; 89 48); opaque = NO; layer = <CALayer:0x7f921b42a330>>",
"<UITabBarButton: 0x7f921b42a700;frame = (283 1; 90 48); opaque = NO; layer = <CALayer:0x7f921b42a900>>"
)
结论:
UITabBarController的UITabBarButton的个数与UITabBarController的子控制器的个数一致,当有多个子控制器的时候:
1> 相邻UITabBarButton之间的间距为4;
2> UITabBarButton与UITabBarController的view之间的间距为2;
3> 每个UITabBarButton的顶部与UITabBar的顶部距离为1;
4> 每个UITabBarButton的高都是48;
5> 每个UITabBarButton的宽基本都是
(375 – 2*2 – 4*(子控制器的个数-1))/ 子控制器的个数;
等价于
(375 – 4*子控制器的个数)/ 子控制器的个数;
不过,UITabBarButton的宽在个别情况下例外,也就是上面的公式结果有余数的时候,比如下图中第三个UITabBarButton的宽度为89,这是苹果内部处理的。
3 两个badge的区别
UITabBarItem的badgevalue是nsstring类型;
@interfaceUITabBarItem : UIBarItem
@property(nonatomic,copy)NSString *badgeValue;
而UIApplication的applicationIconBadgeNumber是NSInteger类型。
@interfaceUIApplication : UIResponder
@property(nonatomic)NSInteger applicationIconBadgeNumber;
badgeValue的显示问题
UITabBarItem的badgeValue可以显示多位数的数字,当 <= 8位数的时候可以正常显示,超过8位就会显示很难看,对比如下:
只要UITabBarItem的image属性没有设置,badgeValue就会显示在对应UITabBarButton左上角,如下图最后一个UITabBarButton。
4 UITabBar和UINavigationBar的高度
UITabBar的默认高度:49
UINavigationBar的默认高度:44
5 UITabBarController的View结构
从最上面到最下面依次显示的是:
UITabBar
子控制器的View
UITabBarController的View
如图: