UITabBarController
//标签栏控制器有一个标签条 是属于标签栏控制器的UITabBarController
//标签条上面的按钮tabBarItem 是属于每个界面的
HomeViewController *homeVC = [[HomeViewController alloc]init];
//tabBarItem标签项
homeVC.tabBarItem.image = [UIImage imageNamed:@"home_tabbar_icon_home"];
homeVC.title = @"首页";
SortViewController *sortVC = [[SortViewController alloc]init];
//tabBarItem未选中的图片
sortVC.tabBarItem.image = [UIImage imageNamed:@"home_tabbar_icon_channel"];
//tabBarItem下面的标题
sortVC.title = @"分类";
//tabBarItem选中的图片
sortVC.tabBarItem.selectedImage = [UIImage imageNamed:@"home_tabbar_icon_channel_sel"];
FindViewController *findVC = [[FindViewController alloc]init];
findVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"home_tabbar_icon_find"] selectedImage:[UIImage imageNamed:@"home_tabbar_icon_find_sel"]];
MineViewController *mineVC = [[MineViewController alloc]init];
mineVC.title = @"我的";
mineVC.tabBarItem.image = [UIImage imageNamed:@"home_tabbar_icon_user"];
mineVC.tabBarItem.selectedImage = [UIImage imageNamed:@"home_tabbar_icon_user_sel"];
//设置提示红点
// mineVC.tabBarItem.badgeValue = @”2”;
//UITabBarController 标签栏控制器 它也是一个容器 用来管理平级界面之间的切换 继承自UIViewController
//UINavigationController 导航控制器 也是一个容器 用来管理具有明确层次关系的界面之间的切换 继承自UIViewController
UITabBarController *tabBarCon = [[UITabBarController alloc]init];
//把平级界面放到数组里面 viewControllers是UITabBarController的属性
tabBarCon.viewControllers = @[homeVC,sortVC,findVC,mineVC];
//数组中元素的顺序 就是显示的顺序
//tabBar 标签条 属于UITabBarController
//barStyle标签条的风格
// tabBarCon.tabBar.barStyle = UIBarStyleBlack;
/*
UIBarStyleDefault
UIBarStyleBlack
UIBarStyleBlackOpaque = 1, // Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2,
*/
//tabBar背景颜色
// tabBarCon.tabBar.barTintColor = [UIColor cyanColor];
//tabBar背景图片
tabBarCon.tabBar.backgroundImage = [UIImage imageNamed:@”home_tabbar”];
//导航条的高度 64
//标签条的高度 49
//默认选中的界面/默认显示的界面
tabBarCon.selectedIndex = 1;
// tabBarCon.selectedViewController = findVC;
//把标签栏控制器作为window的根试图
self.window.rootViewController = tabBarCon;
[self.window makeKeyAndVisible];
return YES;
1.建立新类型 继承自UITabBarController
2.隐藏系统的标签栏 然后重写
@interface MyTabBarController ()
@end
@implementation MyTabBarController
-(void)create{
RootViewController * rootVC = [[RootViewController alloc] init];
rootVC.title = @”首页”;
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.title = @”第二页”;
ThirdViewController * thirdVC = [[ThirdViewController alloc] init];
thirdVC.title = @”第三页”;
FouthViewController * fouthVC = [[FouthViewController alloc] init];
fouthVC.title = @”第四页”;
LastViewController * lastVC = [[LastViewController alloc] init];
lastVC.title = @”第五页”;
self.viewControllers = @[rootVC,secondVC,thirdVC,fouthVC,lastVC];
}
-(void)createTabBar{
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat heigth = [UIScreen mainScreen].bounds.size.height;
//创建UIView作为标签条
UIView * tabView = [[UIView alloc] initWithFrame:CGRectMake(0, heigth - 44, width, 44)];
tabView.backgroundColor = [UIColor orangeColor];
tabView.tag = 200;
//添加背景图
UIImageView * backImage = [[UIImageView alloc] initWithFrame:tabView.frame];
backImage.image = [UIImage imageNamed:@"naviback_n"];
[tabView addSubview:backImage];
NSArray * titleArr = @[@"I",@"M",@"P",@"Q",@"V"];
for (int i = 0; i < 5; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i * width/5.0, 0, width/5, 44);
//未选中状态
NSString * norName =[NSString stringWithFormat:@"btn%@-n",titleArr[i]];
[btn setBackgroundImage:[UIImage imageNamed:norName] forState:UIControlStateNormal];
//选中状态图片
NSString * selName = [NSString stringWithFormat:@"btn%@-h",titleArr[i]];
[btn setBackgroundImage:[UIImage imageNamed:selName] forState:UIControlStateSelected];
btn.tag= 100+i;
if (btn.tag == 100) {
btn.selected = YES;
}
[btn addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside];
[tabView addSubview:btn];
}
//添加下划线
UIImageView * lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43, width/5, 2)];
lineImage.backgroundColor = [UIColor whiteColor];
lineImage.tag = 99;
[tabView addSubview:lineImage];
[self.view addSubview:tabView];
}
-(void)changeState:(UIButton *)btn{
// for (int i = 100; i< 105; i++){
// UIButton * btn1 = (id)[self.view viewWithTag:i];
// btn1.selected = NO;
// }
// static int value = 100;
// UIButton * btn1 = (id)[self.view viewWithTag:value];
// btn1.selected = NO;
// btn.selected = YES;
// value = btn.tag;
btn.selected = YES;
//tabBar.subviews所有的子视图
UIView * views = [self.view viewWithTag:200];
for (UIView * view in views.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
//强转 只会改变变量类型不能改变对象类型
UIButton * subBtn = (UIButton *)view;
if (subBtn != btn) {
subBtn.selected = NO;
}
}
}
//切换界面
self.selectedIndex = btn.tag-100;
//下划线移动
UIImageView * line = (id)[views viewWithTag:99];
[UIView animateWithDuration:0.5 animations:^{
line.center = CGPointMake(btn.center.x, 43);
}];
}
协议使用
//第一个参数tabBarController 第二参数:是当前选中的界面
点击某个页面会触发这个方法 并且将点击的页面传入 是否可以被选择
- (BOOL)tabBarController:(UITabBarController )tabBarController shouldSelectViewController:(UIViewController )viewController{
NSUInteger num = [tabBarController.viewControllers indexOfObject:viewController];
if (num == 1) {
return NO;
}
return YES;
}