一、自定義
#import <UIKit/UIKit.h>
@interface GTabbarViewController : UITabBarController
@end
#import "GTabbarViewController.h"
#import "PlusViewController.h"
@interface GTabbarViewController ()
@property (nonatomic, strong) UIView *myView;
@end
@implementation GTabbarViewController
-(void)viewDidLoad
{
[super viewDidLoad];
// 創(chuàng)建自己的tabbar
_myView = [[UIView alloc] initWithFrame:self.tabBar.frame];
_myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background"]];
// 移除原來(lái)的tabbar
[self.tabBar removeFromSuperview];
[self.view addSubview:_myView];
NSArray *norImgArr = @[@"icn_home",@"icn_chang",@"icn_bs_tj",@"icn_quan",@"icn_me"];
NSArray *selImgArr = @[@"icn_home_click",@"icn_chang_click",@"icn_bs_tj",@"icn_quan_click",@"icn_me_click"];
// 添加按鈕
for (int i = 0; i < 5; i++)
{
UIButton *_itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
float width = KScreenWidth/5;
_itemBtn.frame = CGRectMake(i*width, 0, width, _myView.frame.size.height);
[_itemBtn setImage:[UIImage imageNamed:[norImgArr objectAtIndex:i]] forState:UIControlStateNormal];
[_itemBtn setImage:[UIImage imageNamed:[selImgArr objectAtIndex:i]] forState:UIControlStateSelected];
_itemBtn.tag = 100 + i;
[_itemBtn addTarget:self action:@selector(itemButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[_myView addSubview:_itemBtn];
// 默認(rèn)選中第一個(gè)item
if (i == 0) {
_itemBtn.selected = YES;
}
}
}
// 點(diǎn)擊按鈕的事件
- (void)itemButtonClick:(UIButton *)btn
{
// 切換按鈕選中的圖片
if (btn.tag != 102) {
btn.selected = YES;
for (int i = 0; i < 5; i ++) {
UIButton *tempBtn = (UIButton *)[_myView viewWithTag:100+i];
if (i != btn.tag-100) {
tempBtn.selected = NO;
}
}
}
switch (btn.tag)
{
case 100:
{
self.selectedIndex = 0;
}
break;
case 101:
{
self.selectedIndex = 1;
}
break;
case 102:
{
//中間頁(yè)面模態(tài)視圖彈出
PlusViewController * vcmid = [[PlusViewController alloc] init];
vcmid.tabbarCtr = self;
vcmid.homeNav = self.homeNav;
vcmid.circleNav = self.circleNav;
UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:vcmid];
navi.navigationBar.hidden = NO;
[self presentViewController:navi animated:YES completion:nil];
}
break;
case 103:
{
self.selectedIndex = 2;
}
break;
case 104:
{
//判斷用戶是否已經(jīng)登錄
User *user = [User shareUser];
if (user.id.length == 0) {
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate logoutSuccessAction];
}
else {
self.selectedIndex = 3;
}
}
break;
default:
break;
}
}
-(void)setHidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed{
_myView.hidden = hidesBottomBarWhenPushed;
}
二、隱藏、顯示
1、在自定義的TabBarController.m里寫如下方法:
-(void)setHidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed{
_myView.hidden = hidesBottomBarWhenPushed;
}
2、在你要隱藏tabbar的界面添加如下兩個(gè)方法:
-(void)viewWillAppear:(BOOL)animated{
self.tabBarController.hidesBottomBarWhenPushed = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
self.tabBarController.hidesBottomBarWhenPushed = NO;
}
大工告成。
補(bǔ)充:
在用上面的隱藏方法之前,我在別的地方看到了另一種方法,雖然也能順利隱藏,但是被坑慘了....
方法如下:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 隱藏
NSArray *array=self.tabBarController.view.subviews;
//NSLog(@"%@",array);
UIView *view=array[1];
view.frame=CGRectMake(0, kScreenHeight, 320, 49); //需要顯示的時(shí)候則把這行改為 view.frame=CGRectMake(0, kScreenHeight-49, 320, 49);
}
我使用了這個(gè)方法之后出現(xiàn)了如下的情況:

8C57EACB-53FB-439B-BAA7-8F4E766C09B3.png
如上圖所示,tabbar的右邊居然變短了,而且也點(diǎn)擊不了最后的按鈕。但是這種情況在4.0屏幕的iPhone5上是不會(huì)出現(xiàn)的,在4.7寸phone6以上的大屏幕才會(huì)出現(xiàn),我也弄不清楚到底是什么情況。最后這個(gè)問題花了我一天的時(shí)間才搞清楚是出在tabbar的隱藏方法上面,換了一種方法就好了。