自定义 Tabbar 控制器中间按钮

####自定义 主控制器 自定义 MainTabBarViewController 继承于 UITabBarController

这个控制器引用 UITabBarControllerDelegate,通过这个代理的方法获取到当前点击的 tabbarItem

self.delegate = self;
复制代码

####添加子控制器

- (void)addChildViewControllers {
 	// 根据需求在这里添加子控制器
    HomeViewController *home = [[HomeViewController alloc] init];
    [self addTabItem:home image:nil selectedImage:nil title:@" 首页"];
    
    // 在中间的位置添加一个空的控制器,这样做是为了让底部的 tabbar 排版更好看,加号按钮刚好在中间的这个 item 位置
    UIViewController *ctrl = [[UIViewController alloc] init];
    [self addTabItem:ctrl image:nil selectedImage:nil title:@" "];
    
    MineViewController *mine = [[MineViewController alloc] init];
    [self addTabItem:mine image:nil selectedImage:nil title:@"我的"];
}
复制代码

添加 tabbar

- (void)addTabItem:(UIViewController *)controller image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title {
    controller.navigationItem.title = title ;
    controller.title = title; //同时设置tabBar和nav的title
    controller.tabBarItem.image = image;
    controller.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    
    // 调整tabBarItem的图片 和 文字的位置
    controller.tabBarItem.imageInsets = UIEdgeInsetsMake(-5, 0, 5, 0);
    controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -5);
    
    [self addChildViewController:controller];
}
复制代码

####自定义加号按钮

-(void)addCenterButtonWithImage:(UIImage*)buttonImage selectedImage:(UIImage*)selectedImage {
    self.plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.plusBtn.clipsToBounds = YES;
    self.plusBtn.layer.cornerRadius = 30;
    [self.plusBtn addTarget:self action:@selector(clickedPlusBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.plusBtn.autoresizingMask = UIViewAutoresizingFlexibleRightMargin
                                    | UIViewAutoresizingFlexibleLeftMargin
                                    | UIViewAutoresizingFlexibleBottomMargin
                                    | UIViewAutoresizingFlexibleTopMargin;
    
    //  设定button大小为适应图片
    self.plusBtn.frame = CGRectMake(0.0, 0.0, 60, 60);
    [self.plusBtn setImage:buttonImage forState:UIControlStateNormal];
    [self.plusBtn setImage:selectedImage forState:UIControlStateSelected];    
    
    //  这个比较恶心  去掉选中button时候的阴影
    self.plusBtn.adjustsImageWhenHighlighted = NO;
    CGPoint center = self.tabBar.center;
    center.y = center.y - 30;
    self.plusBtn.center = center;
    
    //按钮就放在当前主控制器的 view 上
    [self.view addSubview:self.plusBtn];
}
复制代码

按钮点击

-(void)clickedPlusBtn:(id)sender {
    // 点击加号需要的操作
    // 如果要是push 或者 present 新的界面,最好是用根控制器的 navigationControler
}
复制代码

####拦截 tabbar 的点击

//每次点击tabBarItem后触发这个方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (self.selectedIndex == 1) { // 因为加号按钮添加到了下标为 1 的 item 上面,所以如果点击到这个位置,就过滤掉这个 item 的响应
        return;
    }
    self.title  = viewController.title;
}

//点击中间的 tablebar 拦截
// 因为加号按钮添加到了下标为 1 的 item 上面,所以如果点击到这个位置,就过滤掉这个 item 的响应
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (viewController == [tabBarController.viewControllers objectAtIndex:1]) {
        return NO;
    }
    return YES;
}
复制代码

转载于:https://juejin.im/post/5a93c9465188257a5911e8e6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值