自定义TabBar中间的Item


做一个二次开发的项目,之前TabBar只有4个Item(要求有5个,中间的Item不是系统的Item),中间的Item被遗漏


本来想重新自定义一个TabBar的,但是感觉有点麻烦,然后就试出一种简单的方法,和大家分享一下


将原来的TabBar的viewControllers改为5个,中间的Item,可以随便写一个ViewController,将它的title设置为“”(没有效果,只是需要他的空间)

<span style="font-size:18px;">UITabBarController * tabBar=[[UITabBarController alloc]init];</span>
<span style="font-size:18px;">na3.title = @"";
tabBar.viewControllers = [na1,na2,na3,na4,na5];</span>
然后在TabBar上面创建一个Button(可以是任何控件),放在TabBar的中间(控件一定要添加到TabBar上面)

<span style="font-size:18px;">_midImageButton = [[UIButton alloc]initWithFrame:CGRectMake(HRScreenW/5*2, 0, HRScreenW/5, self.tabBar.frame.size.height)];
[_midImageButton setImage:[UIImage imageNamed:@"mid"] forState:UIControlStateNormal];
[tabBar addSubview:_midImageButton];</span>

然后遵守UITabBarControllerDelegate,执行下面的方法

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    //如果是点击中间的那个Item,不让它显示对应的ViewController
    UINavigationController * na = (UINavigationController *)viewController;
    if ([na.title isEqualToString:@""]) {
        return NO;
    }
    return YES;
}


然后遵守UITabBarDelegate,执行下面的方法

//选择Item的时候,会执行下面的方法

<span style="font-size:18px;">-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    //如果点击中间那个Item,就弹出一个View
    if ([item.title isEqualToString: @""]) {
        if (_popAddView == nil) {
            _popAddView = [[HRPopAddView alloc]initWithFrame:CGRectMake(0, HRScreenH, HRScreenW, HRScreenH)];
            _popAddView.alpha = 0;
            UIWindow * currentWindow = [UIApplication sharedApplication].keyWindow;
            [currentWindow addSubview:_popAddView];
        }
        [UIView animateWithDuration:0.5 animations:^{
            _popAddView.frame = CGRectMake(0, 0, HRScreenW, HRScreenH);
            _popAddView.alpha = 1;
        }];

    }
}</span>










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值