一.删除系统的标签栏上的按钮(tabbarItem)
//设置tabbar的背景 [self.tabBar setBackgroundImage:[UIImage p_w_picpathNamed:@"tab_bg_all.png"]]; //删除系统的tabbarItem
NSArray *array = self.tabBar.subviews;
//注意:UITabBarButton是一个私有的API,没有公开出来 //遍历tabbar中所有的子视图,并且移除tabbarItem for (UIView *view in array) { Class cls = NSClassFromString(@"UITabBarButton"); if ([view isKindOfClass: cls]) { //移除tabbar上的按钮 [view removeFromSuperview]; } }
//创建自己的图片
//创建选择视图 _selectedImgView = [[UIImageView alloc] initWithImage:[UIImage p_w_picpathNamed:@"selectTabbar_bg_all1"]]; _selectedImgView.frame = CGRectMake(0, 0, 49, 49); [self.tabBar addSubview:_selectedImgView]; //创建按钮 NSArray *imgArr = @[@"movie_home.png", @"msg_new.png", @"start_top250.png", @"icon_cinema.png", @"more_setting.png"]; NSArray *titleArrray = @[@"电影", @"新闻", @"top", @"影院", @"更多"]; CGFloat width = kScreenWidth / imgArr.count; CGFloat height = self.tabBar.height; for (int i = 0; i < imgArr.count; i++) { NSString *imgName = imgArr[i];
NSString *title = titleArrray[i];
MainTabbarItem *item = [[MainTabbarItem alloc] initWithFrame:CGRectMake(width * i, 0, width, height) p_w_picpathName:imgName title:title]; item.tag = 2015 + i; [item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
[self.tabBar addSubview:item];
if (i == 0) { _selectedImgView.center = item.center;
}
当push到二级界面时,隐藏标签栏
在自定义初始化方法中
self.hidesBottomBarWhenPushed = YES;
二,情况二,隐藏系统的标签栏,自定义标签栏
self.tabBar.hidden = YES;
_taBarView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kScreenHeight-55, kScreenWidth, 55)];
当push到二级界面时,隐藏标签栏
#pragma mark - UINavigationControllerDelegate//进入到二级界面以后隐藏标签栏(自定义的)- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ //获取标签栏 MainTabBarViewController *mainBar = (MainTabBarViewController *)self.tabBarController; //当前控制器的个数 NSInteger count = self.viewControllers.count; if (count == 1) { //显示标签栏 [UIView animateWithDuration:0.2 animations:^{ mainBar.taBarView.right = kScreenWidth; }]; }else if (count == 2){ [UIView animateWithDuration:0.2 animations:^{ mainBar.taBarView.right = 0; }]; } }