教你十分鐘搭建一個很好的tabbar模式的項目(純代碼)

先來看一下效果圖:

1.支持導(dǎo)航欄顏色自定義
2.支持返回按鈕自定義
3.支持導(dǎo)航欄右側(cè)按鈕自定義
4.支持導(dǎo)航欄標題自定義
5.支持所有頁面手勢側(cè)滑
6.支持tabbar item 文字顏色自定義(選中狀態(tài)和非選中狀態(tài))


  • 1.導(dǎo)入原資源,如tabbar的items圖片等
  • 2.創(chuàng)建.pch文件

在項目->Bulid Settings->搜prefix header->在后面加入你的.pch文件的絕對路徑
(一個簡單方法就是點出prefix header后面的框后,把工程目錄里面的.pch文件拖到框里,就自動生成它的絕對路徑了)

  • 3.導(dǎo)入需要使用的三方庫MLNavigationController,這個控制器繼承自UINavigationController,加入了返回手勢也可以用系統(tǒng)的導(dǎo)航欄,系統(tǒng)的導(dǎo)航欄現(xiàn)在也自帶返回滑動手勢
  • 4.創(chuàng)建繼承于UITabBarController的tabbar控制器
  • 5.AppDelegate中初始化
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];           
self.window.rootViewController = [[SJTabBarViewController alloc] init];;
[self.window makeKeyAndVisible];
  • 6.創(chuàng)建基類控制器 如:SJBaseViewController。在基類控制器里面主要設(shè)置導(dǎo)航欄及其一些屬性
    ~6.1 基類 .h文件



    ~6.2 基類 .m文件
    //設(shè)置導(dǎo)航欄顏色

 - (void)setNavBarStyle
{
        self.titleViewLB.textColor = color_whiteColor;
        self.navigationController.navigationBar.barTintColor = kNavigationBarColor;
        self.navigationController.navigationBar.translucent = NO;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        UINavigationBar *navigationBar = self.navigationController.navigationBar;
        [navigationBar setBackgroundImage:[UIImage imageNamed:@"account_headBg"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
        [navigationBar setShadowImage:[UIImage new]];
}

//baseBack方法

 - (void)baseBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

//setter and getter方法

 - (UILabel *)titleViewLB
{
    if (_titleViewLB == nil) {
     _titleViewLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth-140, 44)];
     _titleViewLB.backgroundColor = [UIColor clearColor];
     _titleViewLB.textColor = color_whiteColor
     _titleViewLB.font = XSBlodFont(19);
     _titleViewLB.textAlignment = NSTextAlignmentCenter;
    }
    return _titleViewLB;
}

 - (void)setNavTitle:(NSString *)navTitle{
    _titleViewLB.text = navTitle;
}

 - (void)setNavTitleColor:(UIColor *)navTitleColor{
    _titleViewLB.textColor = navTitleColor;
}

 - (void)setShowBack:(BOOL)showBack
{
    if (showBack) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _backButton.frame = CGRectMake(0, 0, 20, 44);
        [_backButton setImage:[UIImage imageNamed:@"back-white"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(baseBack) forControlEvents:UIControlEventTouchUpInside];
        _backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:_backButton];
        backItem.style = UIBarButtonItemStylePlain;
        self.navigationItem.leftBarButtonItem = backItem;
    }else{
        self.navigationItem.hidesBackButton = YES;
    }
    //解決titleView不能居中的問題
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 35, 35);
    button.hidden = YES;
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = right;
}

 - (void)setShowLeftButton:(BOOL)showLeftButton
{
    if (showLeftButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 35, 35);
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        [button addTarget:self action:@selector(leftBarButtonItemTouchedUpInSide) forControlEvents:UIControlEventTouchUpInside];
    }
}

 - (void)setShowRightButton:(BOOL)showRightButton
{
    if (showRightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(0, 0, 30, 30);
        _rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
        //可以根據(jù)自己想要的圖標在目標控制器里設(shè)置
//        [_rightButton setImage:[UIImage imageNamed:@"more_icon_forum"] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
        [_rightButton addTarget:self action:@selector(rightBarButtonItemTouchedUpInSide) forControlEvents:UIControlEventTouchUpInside];
    }
}

//touch up inside響應(yīng)按鈕方法

 - (void)leftBarButtonItemTouchedUpInSide
{
    //根據(jù)自己需要實現(xiàn)
    NSLog(@"leftBarButtonItemTouchedUpInSide未實現(xiàn)");
}

 - (void)rightBarButtonItemTouchedUpInSide
{
    //根據(jù)自己需要實現(xiàn)
    NSLog(@"rightBarButtonItemTouchedUpInSide未實現(xiàn)");
}
  • 7.創(chuàng)建tabbar Items 控制器,tabbar有幾個items就創(chuàng)建幾個,繼承自BaseViewController

  • 8.創(chuàng)建tabbar控制器,繼承自UITabBarController
    ~8.1 遵守協(xié)議:UITabBarDelegate,UITabBarControllerDelegate
    ~8.2 items控制器組

    @property(nonatomic,strong)NSMutableArray *viewControllers;

~8.3 初始化控制器

 - (void)setUpControllers
{
    SJOneViewController * oneVC = [[SJOneViewController alloc] init];
    MLNavigationController *oneNav = [[MLNavigationController alloc] initWithRootViewController:oneVC];
    SJTwoViewController * twoVC = [[SJTwoViewController alloc] init];
    MLNavigationController *twoNav = [[MLNavigationController alloc] initWithRootViewController:twoVC];
    SJThreeViewController *threeVC = [[SJThreeViewController alloc] init];
    MLNavigationController *threeNav = [[MLNavigationController alloc] initWithRootViewController:threeVC];
    [self.viewControllers addObject:oneNav];
    [self.viewControllers addObject:twoNav];
    [self.viewControllers addObject:threeNav];
}

~8.4 添加子控制器,初始化tabbarItem 方法

 - (void)addController:(UIViewController *)viewController title:(NSString *)title normolImageName:(NSString *)normalImageName selectImageName:(NSString *)selectImageName
{
    viewController.tabBarItem.title = title;
    
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    normalImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.image = normalImage;
    
    UIImage *selectedImage = [UIImage imageNamed:selectImageName];
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = selectedImage;
    
    [self addChildViewController:viewController];
}

~8.5 設(shè)置文字不同狀態(tài)下屬性方法

 - (void)setUpTabbarItemTextAttributes
{
    //普通狀態(tài)下的文字屬性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.00f green:0.64f blue:0.51f alpha:1.00f];
    //選中狀態(tài)下的文字屬性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    UITabBarItem *tabbarItem = [UITabBarItem appearance];
    [tabbarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabbarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

~8.6 UITabBarControllerDelegate代理方法(可選實現(xiàn))

 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([viewController isKindOfClass:[UINavigationController class]]) {
     UINavigationController *nav = (UINavigationController *)viewController;
     UIViewController *vc = [nav.viewControllers objectAtIndex:0];
     NSLog(@"current viewcontroller is %@", vc);
    }
    return YES;
}

~8.7 tabbar items 各屬性賦值(核心代碼示例)

NSArray *array = @[@{@"title":@"殺敵",@"normalImageName":@"battle_win_kill",@"selectImageName":@"battle_lose_kill"},
                       @{@"title":@"死亡",@"normalImageName":@"battle_win_death",@"selectImageName":@"battle_lose_death"},
                       @{@"title":@"助攻",@"normalImageName":@"battle_win_assist",@"selectImageName":@"battle_lose_assist"}];
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dic = (NSDictionary *)obj;
        UIViewController *vc = self.viewControllers[idx];
        NSString *title = [dic objectForKey:@"title"];
        NSString *normalImageName = [dic objectForKey:@"normalImageName"];
        NSString *selectImageName = [dic objectForKey:@"selectImageName"];
        
        [self addController:vc title:title normolImageName:normalImageName selectImageName:selectImageName];
    }];
    [self setUpTabbarItemTextAttributes];
    self.delegate = self;
  • 9.至此基本完畢,在目標控制器里面,可以選擇性的實現(xiàn)基類的一些方法來自定義目標控制器,如:
    ①目標控制器的navTitle
    <pre>[self setNavTitle:@"殺敵"];</pre>
    ②是否有返回按鈕,默認無
    <pre>[self setShowRightButton:YES];</pre>
    ③設(shè)置navigationItem.rightBarButtonItem的圖標
    <pre>[self.rightButton setImage:[UIImage imageNamed:@"battle_win_death"] forState:UIControlStateNormal];</pre>
    ④rightBarButtonItem的點擊響應(yīng)放法
    <pre>- (void)rightBarButtonItemTouchedUpInSide{
    //your code
    }</pre>
文章為作者辛苦碼出來的,轉(zhuǎn)載請注明出處,喜歡的話請star??一下。示例代碼鏈接:https://github.com/SPIREJ/SJTabbarProject
最后編輯于
?著作權(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)容

  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,737評論 1 14
  • { 11、核心動畫 需要簽協(xié)議,但是系統(tǒng)幫簽好 一、CABasicAnimation 1、創(chuàng)建基礎(chǔ)動畫對象 CAB...
    CYC666閱讀 1,703評論 2 4
  • //設(shè)置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 977評論 0 0
  • 赤裸裸的被人稱贊,那種美妙的感覺久遠的快憶不起來了.還好,昨天一個天籟般的聲音,讓我重拾美妙,瞬間激動的一塌糊涂....
    銀子姐閱讀 369評論 11 5
  • 此時此刻不知道該分享什麼。記錄下今天的感受感悟。 今天一整天的服務(wù)下來,有種感受,眼見不一定為實,耳聽不見得是真…...
    粟莎閱讀 178評論 0 1

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