1.每個(gè)UIViewController都有一個(gè)tabBarItem
@interface UIViewController (UITabBarControllerItem)
@property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem;
2.UITabBarItem(子類)
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem
3.UIBarItem(父類)
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject <NSCoding, UIAppearance>
// 設(shè)置tabBarItem的文字
@property(nullable, nonatomic,copy) NSString *title; // default is nil
4.示例代碼
- 示例代碼1
//創(chuàng)建子控制器
UIViewController *c1=[[UIViewController alloc]init];
c1.view.backgroundColor=[UIColor grayColor];
c1.view.backgroundColor=[UIColor greenColor];
c1.tabBarItem.title=@"消息";
c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
c1.tabBarItem.badgeValue=@"123";
- 示例代碼2
/**
往tabbar添加一個(gè)子控制器
@param childController 子控制器
@param title 子控制器的title
@param image tabbar的圖片名字
*/
- (void)addChildViewController:(UIViewController *)childController title: (NSString *)title image: (NSString *)image {
childController.title = title;
// 設(shè)置tabbarItem的文字大小和顏色
[childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: globalColor} forState:UIControlStateSelected];
[childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal];
// 設(shè)置tabbar的圖片
// 如果沒(méi)有傳圖片(中間的占位控制器), 就不設(shè)置tabbar的image
if (image != nil) {
childController.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@", image]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@_selected", image]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:childController];
[self addChildViewController:navController];
}
- 示例代碼3
// 設(shè)置tabbarItem的文字大小和顏色
[childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: globalColor} forState:UIControlStateSelected];
[childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal];
- 很多屬性的設(shè)置都可以在這個(gè)文件里找到
NSAttributedString.h
5.添加子控制器到UITabBarController中
- 方式一
[tb addChildViewController:c1];
[tb addChildViewController:c2];
- 方式二
tb.viewControllers=@[c1,c2,c3,c4];
6.UITabBar
下方的工具條稱為UITabBar 洛波,如果UITabBarController有N個(gè)子控制器,那么UITabBar內(nèi)部就會(huì)有N 個(gè)UITabBarButton作為子控件與之對(duì)應(yīng)泛烙。
注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度為49