自定义UITabbar(创建、隐藏)

一、自定义

#import 
@interface GTabbarViewController : UITabBarController
@end
#import "GTabbarViewController.h"
#import "PlusViewController.h"

@interface GTabbarViewController ()

@property (nonatomic, strong) UIView *myView;

@end

@implementation GTabbarViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    // 创建自己的tabbar
    _myView = [[UIView alloc] initWithFrame:self.tabBar.frame];
    _myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background"]];
    // 移除原来的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];
        // 默认选中第一个item
        if (i == 0) {
            _itemBtn.selected = YES;
        }
    }
}
// 点击按钮的事件
- (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:
        {
            //中间页面模态视图弹出
            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:
        {
            //判断用户是否已经登录
            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的界面添加如下两个方法:

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

大工告成。

补充:

在用上面的隐藏方法之前,我在别的地方看到了另一种方法,虽然也能顺利隐藏,但是被坑惨了....
方法如下:

-(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); //需要显示的时候则把这行改为 view.frame=CGRectMake(0, kScreenHeight-49, 320, 49);
}

我使用了这个方法之后出现了如下的情况:


自定义UITabbar(创建、隐藏)_第1张图片
8C57EACB-53FB-439B-BAA7-8F4E766C09B3.png

如上图所示,tabbar的右边居然变短了,而且也点击不了最后的按钮。但是这种情况在4.0屏幕的iPhone5上是不会出现的,在4.7寸phone6以上的大屏幕才会出现,我也弄不清楚到底是什么情况。最后这个问题花了我一天的时间才搞清楚是出在tabbar的隐藏方法上面,换了一种方法就好了。

参考:https://my.oschina.net/u/2461772/blog/508254?p=1

你可能感兴趣的:(自定义UITabbar(创建、隐藏))