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

先來(lái)看一下效果圖:

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


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

在項(xiàng)目->Bulid Settings->搜prefix header->在后面加入你的.pch文件的絕對(duì)路徑
(一個(gè)簡(jiǎn)單方法就是點(diǎn)出prefix header后面的框后,把工程目錄里面的.pch文件拖到框里,就自動(dòng)生成它的絕對(duì)路徑了)

  • 3.導(dǎo)入需要使用的三方庫(kù)MLNavigationController,這個(gè)控制器繼承自UINavigationController,加入了返回手勢(shì)也可以用系統(tǒng)的導(dǎo)航欄,系統(tǒng)的導(dǎo)航欄現(xiàn)在也自帶返回滑動(dòng)手勢(shì)
  • 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)建基類(lèi)控制器 如:SJBaseViewController。在基類(lèi)控制器里面主要設(shè)置導(dǎo)航欄及其一些屬性
    ~6.1 基類(lèi) .h文件



    ~6.2 基類(lèi) .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不能居中的問(wèn)題
    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ù)自己想要的圖標(biāo)在目標(biāo)控制器里設(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ù)自己需要實(shí)現(xiàn)
    NSLog(@"leftBarButtonItemTouchedUpInSide未實(shí)現(xiàn)");
}

 - (void)rightBarButtonItemTouchedUpInSide
{
    //根據(jù)自己需要實(shí)現(xiàn)
    NSLog(@"rightBarButtonItemTouchedUpInSide未實(shí)現(xiàn)");
}
  • 7.創(chuàng)建tabbar Items 控制器,tabbar有幾個(gè)items就創(chuàng)建幾個(gè),繼承自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代理方法(可選實(shí)現(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.至此基本完畢,在目標(biāo)控制器里面,可以選擇性的實(shí)現(xiàn)基類(lèi)的一些方法來(lái)自定義目標(biāo)控制器,如:
    ①目標(biāo)控制器的navTitle
    <pre>[self setNavTitle:@"殺敵"];</pre>
    ②是否有返回按鈕,默認(rèn)無(wú)
    <pre>[self setShowRightButton:YES];</pre>
    ③設(shè)置navigationItem.rightBarButtonItem的圖標(biāo)
    <pre>[self.rightButton setImage:[UIImage imageNamed:@"battle_win_death"] forState:UIControlStateNormal];</pre>
    ④rightBarButtonItem的點(diǎn)擊響應(yīng)放法
    <pre>- (void)rightBarButtonItemTouchedUpInSide{
    //your code
    }</pre>
文章為作者辛苦碼出來(lái)的,轉(zhuǎn)載請(qǐng)注明出處,喜歡的話(huà)請(qǐng)star??一下。示例代碼鏈接:https://github.com/SPIREJ/SJTabbarProject
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,698評(píng)論 6 539
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,202評(píng)論 3 426
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事?!?“怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 177,742評(píng)論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 63,580評(píng)論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,297評(píng)論 6 410
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 55,688評(píng)論 1 327
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,693評(píng)論 3 444
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 42,875評(píng)論 0 289
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,438評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,183評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,384評(píng)論 1 372
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,931評(píng)論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,612評(píng)論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 35,022評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 36,297評(píng)論 1 292
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,093評(píng)論 3 397
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,330評(píng)論 2 377

推薦閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,512評(píng)論 1 14
  • { 11、核心動(dòng)畫(huà) 需要簽協(xié)議,但是系統(tǒng)幫簽好 一、CABasicAnimation 1、創(chuàng)建基礎(chǔ)動(dòng)畫(huà)對(duì)象 CAB...
    CYC666閱讀 1,575評(píng)論 2 4
  • //設(shè)置尺寸為屏幕尺寸的時(shí)候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 830評(píng)論 0 0
  • 赤裸裸的被人稱(chēng)贊,那種美妙的感覺(jué)久遠(yuǎn)的快憶不起來(lái)了.還好,昨天一個(gè)天籟般的聲音,讓我重拾美妙,瞬間激動(dòng)的一塌糊涂....
    銀子姐閱讀 332評(píng)論 11 5
  • 一、CoreAnimation的介紹 Core Animation是一組非常強(qiáng)大的動(dòng)畫(huà)處理API,使用它能做出非常...
    任性不認(rèn)命ToT閱讀 348評(píng)論 0 0