一.刪除系統的標籤欄上的按鈕(tabbarItem)
//設置tabbar的背景
[self.tabBar setBackgroundImage:[UIImage imageNamed:@"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 imageNamed:@"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)
imageName: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;
}];
}
}