//当分栏控制器内的模块 超过五个 的时候 会只显示4个 其他的都放在more分栏中
//tabbr的高度是49
//背景颜色
tabbarcontronller.tabBar.barTintColor=[UIColor orangeColor];
//背景图
[tabbarcontronller.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_bg.png"]];
//镂空颜色
//tabbarcontronller.tabBar.tintColor=[UIColor greenColor];
//设置tabbarItem
//单独设置文字图片 及选中图片
//第一模块
UINavigationController*nvc=tabarcontronller.viewControllers[0];
//若没有设置这个属性 默认显示视图器的标题
nvc.tabBarItem.title=@"模块一";
nvc.tabBarItem.image=[UIImage imageNamed:@"tabbar_account"];
nvc.tabBarItem.selectedImage=[[UIImage imageNamed:@"tabbar_account_press"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
];
//系统样式
//第二模块
RootViewController * rvc=tabarcontronller.viewControllers[1];
/**
UITabBarSystemItemMore, 等等 UITabBarSystemItemFavorites,星星
UITabBarSystemItemFeatured, 星星
UITabBarSystemItemTopRated,星星
UITabBarSystemItemRecents, 时钟
UITabBarSystemItemContacts, 人头
UITabBarSystemItemHistory,时钟
UITabBarSystemItemBookmarks, 书签
UITabBarSystemItemSearch,搜索
UITabBarSystemItemDownloads, 下载
UITabBarSystemItemMostRecent,
UITabBarSystemItemMostViewed,
*/
rvc.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem: UITabBarSystemItemDownloads tag:2];
//第三模块
//3设置标题和图片
UINavigationController*nvc_three=tabarcontronller.viewControllers[2];
nvc_three.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"ModelThree" image:[[UIImage imageNamed:@"tabbar_appfree"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
] tag:3];
//4设置标题 图片一级选中图片
RootViewController*rvc_four=tabarcontronller.viewControllers[3];
rvc_four.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"ModelFour" image:[UIImage imageNamed:@"tabbar_limitfree"] selectedImage:[[UIImage imageNamed:@"tabbar_limitfree_press"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
] ];
//设置程序已启动 显示模块的位置 默认为0 设置哪个开启时默认显示那个
tabarcontronller.selectedIndex=3;
//添加徽标
rvc_four.tabBarItem.badgeValue=@"10";
}
//编辑分栏上的模块会触发协议上的方法
-(void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
//viewController 是存放视图器 改变顺序 之后的数组
NSLog(@"%@",viewControllers);
}
//点击某一模块的方法
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if (viewController.tabBarItem.tag==3) {
return NO;
}
//返回Yes 是允许点击 NO是不允许点击
return YES;
}
定义一个视图器数组
NSMutableArray * viewController=[[NSMutableArray alloc]init];
将类的名字装进数组
NSArray * classNameArray=[NSArray arrayWithObjects:@"AllShowViewController",@"ReceiveValueTableViewController",@"ShowButtonViewController", nil];
将图片装进数组
NSArray*imageArray=[NSArray arrayWithObjects:@"tabbar_account",@"tabbar_appfree",@"tabbar_limitfree" ,nil];
将选中的图片装进数组
NSArray*selectedImageArray=[NSArray arrayWithObjects:@"tabbar_account_press",@"tabbar_appfree_press",@"tabbar_limitfree_press", nil];
将tabbar的标题装进数组
NSArray* titleArray=[NSArray arrayWithObjects:@"模块一",@"模块二",@"模块三", nil];
定义一个标识
int i=0;
遍历类名数组
for (NSString*className in classNameArray) {
Class class=NSClassFromString(className);
RootControler是所有视图类父类
RootViewController*nvc=[[class alloc]init];
将导航视图器指向父类对象
UINavigationController* unc=[[UINavigationController alloc]initWithRootViewController:nvc];
遍历取得名字图片选中图片
nvc.tabBarItem.title=titleArray[i];
nvc.tabBarItem.image=[[UIImage imageNamed:imageArray[i]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nvc.tabBarItem.selectedImage=[[UIImage imageNamed:selectedImageArray[i]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
将导航视图器装进视图器数组
[viewController addObject:unc];
i++;
}
将当前的视图控制器设置为数组视图器
self.viewControllers=viewController;
tabBar
WPJViewController5 *vc = [[WPJViewController5 alloc] init];
vc.hidesBottomBarWhenPushed = NO;(隐藏tabbar)
// 通过点击, 改变tab的选择
// 1. 拿到tabBarController
UITabBarController *tab = self.tabBarController;
// 2. 修改index
tab.selectedIndex = 0;(直接跳到第0页)
// 创建 tabBarController
// tabBar也是一个容器视图控制器, 用来管理视图的
UITabBarController *tab = [[UITabBarController alloc] init];
// 添加管理的 ViewControllers
// 添加进去管理的viewController, 一开始并没有显示出来, 所以, 不会去走ViewDidLoad
// 最多只会显示5个, 如果超出5个, 显示4个, 其他的放在more里面
// 实际工作中, 没有这样的设计
tab.viewControllers = vcs;
// tab关联到window上
self.window.rootViewController = tab;
//[self property:tab];
}
- (void)property:(UITabBarController *)tab {
// 1. tabBar的背景是半透明的, 修改是否透明
tab.tabBar.translucent = YES;
// 2. tabBar的风格
/**
UIBarStyleDefault = 0, // 白色
UIBarStyleBlack = 1, // 黑色
UIBarStyleBlackOpaque = 1, // 顶楼上
UIBarStyleBlackTranslucent = 2, // 黑色+透明
*/
tab.tabBar.barStyle = UIBarStyleBlack;
// 3. 镂空颜色
tab.tabBar.tintColor = [UIColor greenColor];
// 4. tabBar的背景颜色(同样适用于导航栏)
if ([UIDevice currentDevice].systemVersion.floatValue >= 7) {
tab.tabBar.barTintColor = [UIColor greenColor];
}
// 5. 背景图片
tab.tabBar.backgroundImage = [UIImage imageNamed:@"header_bg"];
// 6. tabBarItem
NSArray *vcs = tab.viewControllers;
WPJRootViewController *vc0 = vcs[0];
// 6.1 设置系统风格的tabBarItem
/**
UITabBarSystemItemMore,
UITabBarSystemItemFavorites,
UITabBarSystemItemFeatured,
UITabBarSystemItemTopRated,
*/
vc0.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:8];
// 6.2 带有标题, 图片的tabBarItem
WPJRootViewController *vc1 = vcs[1];
// 处理图片, 保留原色
UIImage *image = [UIImage imageNamed:@"tab_0"];
if ([UIDevice currentDevice].systemVersion.floatValue >= 7) {
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
vc1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"hello" image:image tag:9];
// 6.3 带有标题, 图片, 选中图片的tabBarItem
WPJRootViewController *vc = vcs[2];
UIImage *imageH = [UIImage imageNamed:@"tab_c0"];
// imageH = [self originalImage:imageH];
// [self customImage:imageH];
// 类别
imageH = [imageH orginalImage];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"hello2" image:image selectedImage:imageH];
// 协议方法
tab.delegate = self;
}
// 将要选择指定的ViewController的回调
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
// viewController表示将要被选中的视图控制器
// 如果将要被选中的视图控制器的 tagBarItem.tag 为9, 不允许被选择
if (viewController.tabBarItem.tag == 9) {
return NO;
}
return YES;
}