步骤1
新建tabbarVC继承UITabBarControllerVC
自定义tabbarView // 准备盖到tabbar上面,盖住他
步骤二
#import "DFTabView.h"
@interface DFTabVC ()<UITabBarControllerDelegate>
@property(nonatomic, strong) DFTabView *tabView;
@end
@implementation DFTabVC
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 系统不支持6个tab,要自己弄个view盖上去
// 延迟0.1的原因是超过6个tab后, 系统会自定义一个tab加到view里面去,延迟0.1再创建可以盖到新的view上面去
[self replaceTab];
});
}
- (void)replaceTab{
//去掉原生tabbar分割线
[self.tabBar setShadowImage:[UIImage new]];
[self.tabBar setBackgroundImage:[UIImage new]];
//添加自定义customTabBar到系统tabBar上面
[self.tabBar addSubview:self.tabView];
}
- (DFTabView *)tabView{
if (!_tabView) {
_tabView = [DFTabView loadFromNib];
_tabView.frame = CGRectMake(0, 0, IPHONE_WIDTH, TABBAR_HEIGHT + 7);
_tabView.autoresizingMask = UIViewAutoresizingNone;
_tabView.selectIndex = 0;
_tabView.backgroundColor = [UIColor whiteColor];
DFWeakSelf
_tabView.callback = ^(NSInteger row) {
// 自己调用tab的selectedIndex去修改选中页面
if (weakSelf.selectedIndex == row) {
// 通知vc,tab已经被点击,超过后会有moreNavVC,所以需要将nav选中第二个vc
[[DFNotificationCenter defaultCenter] postNotificationByName:EVEN_TABARSELECT info:@(row)];
}
weakSelf.selectedIndex = row;
};
}
return _tabView;
}
步骤3
// 在所在的vc,监听tab点击事件
[[DFNotificationCenter defaultCenter] addEventName:EVEN_TABARSELECT observer:self event:^(id info) {
NSInteger selectTag = [info integerValue];
if (selectTag == self.tag) {
// 这个是我公司的需求,点击tab回到首页,如果只是首页才有tabbarVC,可以不用加这个判断
if (self.tag < 4) {
[weakSelf.navigationController popToRootViewControllerAnimated:YES];
}else{
[weakSelf.navigationController popToViewController:self.navigationController.viewControllers[1] animated:NO];
}
}
}];
![image.png](https://img.haomeiwen.com/i3445110/845bb4421aec84c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
网友评论