iOS Tabbar超過5個的解決方案(非復(fù)制)

步驟1

新建tabbarVC繼承UITabBarControllerVC
自定義tabbarView // 準(zhǔn)備蓋到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(), ^{
        // 系統(tǒng)不支持6個tab,要自己弄個view蓋上去
        // 延遲0.1的原因是超過6個tab后, 系統(tǒng)會自定義一個tab加到view里面去,延遲0.1再創(chuàng)建可以蓋到新的view上面去
        [self replaceTab];
    });
}

- (void)replaceTab{

       //去掉原生tabbar分割線
       [self.tabBar setShadowImage:[UIImage new]];
       [self.tabBar setBackgroundImage:[UIImage new]];

       //添加自定義customTabBar到系統(tǒng)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) {
            // 自己調(diào)用tab的selectedIndex去修改選中頁面
            if (weakSelf.selectedIndex == row) {
             // 通知vc,tab已經(jīng)被點擊,超過后會有moreNavVC,所以需要將nav選中第二個vc
                [[DFNotificationCenter defaultCenter] postNotificationByName:EVEN_TABARSELECT info:@(row)];
            }
            weakSelf.selectedIndex = row;
        };
    }
    return _tabView;
}

步驟3
//  在所在的vc,監(jiān)聽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://upload-images.jianshu.io/upload_images/3445110-845bb4421aec84c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容