【iOS UI】自定義TabBar

自定義TabBar

一、XTabBarButton

XTabBarButton.h  

#import <UIKit/UIKit.h>

@interface XTabBarButton : UIButton

//  標(biāo)簽
@property (nonatomic, strong) UILabel *label;
//  按鈕
@property (nonatomic, strong) UIButton *btn;

@end
XTabBarButton.m
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kTabBarWidth kScreenWidth/3

#import "XTabBarButton.h"


@implementation XTabBarButton

#pragma mark - 創(chuàng)建子控件
- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {
        
        //  1爪幻、創(chuàng)建標(biāo)簽
        _label = [[UILabel alloc] init];
        _label.frame = CGRectMake(0, 49-15, kTabBarWidth, 15);
        _label.textAlignment = NSTextAlignmentCenter;
        _label.backgroundColor = [UIColor yellowColor];
        _label.font = [UIFont systemFontOfSize:12];
        [self addSubview:_label];
        
        //  2、創(chuàng)建按鈕
        _btn = [[UIButton alloc] init];
        _btn.backgroundColor = [UIColor yellowColor];
        _btn.frame = CGRectMake((kTabBarWidth-25)/2, 5, 25, 25);
        [self addSubview:_btn];
        
    }
    return self;
}

@end

二、XTabBar

XTabBar.h
#import <UIKit/UIKit.h>

//導(dǎo)入自定義的XTabBarButton
#import "XTabBarButton.h"  

//子控制器數(shù)量=3
#define ChildControllerCount 3

@class XTabBar;

//delegate
@protocol XTabBarDelegate <NSObject>

//delegate methods
//  點(diǎn)擊XTabBarButton后淳附,tabbarController執(zhí)行該方法
- (void)xTabbarClickBtnFrom:(NSInteger)from to:(NSInteger)to;

@end


@interface XTabBar : UIView  //繼承自UIView

@property (nonatomic, weak) id<XTabBarDelegate> delegate;    //代理
@property (nonatomic, strong) NSArray *tabBarTitleArr;    //自定義XTabBar上的標(biāo)題數(shù)組
@property (nonatomic, strong) NSArray *tabBarNormalImgArr;    //自定義XTabBar上的正常狀態(tài)圖標(biāo)數(shù)組
@property (nonatomic, strong) NSArray *tabBarSelectedImgArr;    //自定義XTabBar上的點(diǎn)擊狀態(tài)圖標(biāo)數(shù)組
@property (nonatomic, strong) NSMutableArray *btnArr;   //存放自定義按鈕的可變數(shù)組
@property (nonatomic, strong) UIButton *selectedBtn;    //點(diǎn)擊了的按鈕

//  添加自定義按鈕
- (void)addBBIwithTitle:(NSString *)title NormalImageName:(NSString *)normalImage SelectedImageName:(NSString *)selectedImageName;

//  新方法設(shè)置tabbar
- (void)setXTabBar;

@end

XTabBar.m
//寬度宏
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
//自定義XTabBarButton的寬度宏
#define kTabBarButtonWidth kScreenWidth/3


#import "XTabBar.h"

@implementation XTabBar

#pragma mark - 懶加載存放按鈕的可變數(shù)組
- (NSMutableArray *)btnArr{
    if (!_btnArr) {
        _btnArr = [[NSMutableArray alloc] init];
    }
    return _btnArr;
}

#pragma mark - 設(shè)置子控件的位置
- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.backgroundColor = [UIColor whiteColor];
    
    int num = 3;
    for (int i = 0; i < num; i++) {
        UIButton *btn = self.subviews[i];
        btn.frame = CGRectMake(i*kTabBarButtonWidth, 0, kTabBarButtonWidth, 49);
        btn.tag = i;    //
    }
    
    //  點(diǎn)擊第二個(gè)按鈕
    if (_btnArr.count == 3) {

        //  1、tabBar拿到中間按鈕
        XTabBarButton *fistPressBtn = [_btnArr objectAtIndex:1];

        //  2蠢古、把中間按鈕傳給點(diǎn)擊方法奴曙,使tabBar控制器直接顯示中間的控制器
        [self xTabBarButtonClickWithBtn:fistPressBtn];
    }
}

#pragma mark - 添加一個(gè)自定義的按鈕
- (void)addBBIwithTitle:(NSString *)title NormalImageName:(NSString *)normalImage SelectedImageName:(NSString *)selectedImageName{
    
    //  1、創(chuàng)建自定義的按鈕
    XTabBarButton *xTabBarButton = [[XTabBarButton alloc] init];
    
    //  2草讶、自定義按鈕的子按鈕與標(biāo)簽不可交互洽糟,設(shè)置label的title
    xTabBarButton.btn.userInteractionEnabled = NO;
    xTabBarButton.label.userInteractionEnabled = NO;
    xTabBarButton.label.text = title;
    
    //  3、自定義按鈕的子按鈕設(shè)置圖片
    [xTabBarButton.btn setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];

    //  4堕战、自定義按鈕綁定方法
    [xTabBarButton addTarget:self action:@selector(xTabBarButtonClickWithBtn:) forControlEvents:UIControlEventTouchDown];
    
    //  5坤溃、沒有title時(shí)
    if (title.length == 0) {
        //  隱藏label
        xTabBarButton.label.alpha = 0;
        
        //  調(diào)整子按鈕的大小
        xTabBarButton.btn.frame = CGRectMake(5, 5, 5, 5);
        
    }
    
    
    //  6、添加自定義的xTabbarButton到自定義tabBar上
    [self addSubview:xTabBarButton];
    [self.btnArr addObject:xTabBarButton];
    
    
}


//  新方法設(shè)置tabbar
- (void)setXTabBar{
    
    _tabBarTitleArr = @[@"資訊",@"",@"我"];
    _tabBarNormalImgArr = @[@"tabbarZixunNormal",@"tabbarHomeNormal",@"tabbarMineNormal"];
    _tabBarSelectedImgArr = @[@"tabbarZixunSelected",@"tabbarHomeSelected",@"tabbarMineSelected"];
    
    for (int i = 0; i < ChildControllerCount; i++) {
        
        NSString *titleString = [NSString stringWithFormat:@"%@", _tabBarTitleArr[i]];
        
        //  1嘱丢、循環(huán)創(chuàng)建按鈕
        XTabBarButton *xTabBarButton = [XTabBarButton new];
        
        //  2薪介、xTabBarButton的子按鈕與標(biāo)簽不可交互
        xTabBarButton.btn.userInteractionEnabled = NO;
        xTabBarButton.label.userInteractionEnabled = NO;
        
        //  3、設(shè)置標(biāo)簽文字與子按鈕圖片
        xTabBarButton.label.text = _tabBarTitleArr[i];
        
        //  3.1屿讽、當(dāng)沒有title時(shí)昭灵,調(diào)整按鈕的位置
        if (titleString.length == 0) {
            xTabBarButton.label.alpha = 0;
//
//            CGPoint center = xTabBarButton.center;
//            NSLog(@"center=%f, %f", center.x, center.y);

            xTabBarButton.btn.frame = CGRectMake((kTabBarButtonWidth-70)/2, -9, 70, 58);


        }
        
        [xTabBarButton.btn setImage:[UIImage imageNamed:_tabBarNormalImgArr[i]] forState:UIControlStateNormal];
        
        
        
        
        //  4、綁定方法
        [xTabBarButton addTarget:self action:@selector(xTabBarButtonClickWithBtn:) forControlEvents:UIControlEventTouchDown];
        
        //  5伐谈、打tag
        xTabBarButton.tag = i;
        
        //  6烂完、添加到自定義按鈕的數(shù)組

        [self.btnArr addObject:xTabBarButton];
        
        //  7、添加創(chuàng)建好的按鈕
        [self addSubview:xTabBarButton];
     
    }
}

#pragma mark - 點(diǎn)擊自定義tabbar中的xTabbarButton
- (void)xTabBarButtonClickWithBtn:(UIButton *)btn{
    
    //  1诵棵、遍歷按鈕數(shù)組中的按鈕抠蚣,改變所有按鈕的圖片和標(biāo)簽顏色
    for (int i = 0; i < _btnArr.count; i++) {
        XTabBarButton *tempBtn = [_btnArr objectAtIndex:i];
        tempBtn.label.textColor = [UIColor colorWithRed:70/255.0 green:130/255.0 blue:180/255.0 alpha:1.0];
        [tempBtn.btn setImage:[UIImage imageNamed:_tabBarNormalImgArr[i]] forState:UIControlStateNormal];
    }
    
    //  2、設(shè)置當(dāng)前按鈕的照片和標(biāo)簽顏色履澳,調(diào)用代理方法
    XTabBarButton *preBtn = [btn viewWithTag:btn.tag];
    preBtn.label.textColor = [UIColor orangeColor];
    [preBtn.btn setImage:[UIImage imageNamed:_tabBarSelectedImgArr[btn.tag]] forState:UIControlStateNormal];
    
    //  3嘶窄、調(diào)用代理方法
    if ([self.delegate respondsToSelector:@selector(xTabbarClickBtnFrom:to:)]) {
        [self.delegate xTabbarClickBtnFrom:2 to:btn.tag];
    }
    
    //  4、更新選中的按鈕
    _selectedBtn = btn;
    
}

@end

三距贷、XTabBarController

XTabBarController.h
#import <UIKit/UIKit.h>
#import "XTabBar.h" //TabBar

@interface XTabBarController : UITabBarController

@property (nonatomic, strong) XTabBar *xTabBar;

@end
XTabBarController.m
#import "XTabBarController.h"

//子控制器個(gè)數(shù) 3個(gè)
#define ChildControllerCount 3

@interface XTabBarController ()<XTabBarDelegate>

@end

@implementation XTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  1柄冲、初始化自定義的tabbar,設(shè)置委托
    _xTabBar = [[XTabBar alloc] initWithFrame:self.tabBar.frame];
    _xTabBar.delegate = self;
    
    //  2忠蝗、添加子控制器 ZixunNavController
    ZixunNavController *zxNC = [[ZixunNavController alloc] initWithRootViewController:[ZixunViewController new]];
    HomeNavController *homeNC = [[HomeNavController alloc] initWithRootViewController:[HomepageViewController new]];
    MineNavController *mineNC = [[MineNavController alloc] initWithRootViewController:[MineViewController new]];
    NSArray *viewControllersArr = @[zxNC, homeNC, mineNC];
    self.viewControllers = viewControllersArr;
    
    //  3现横、自定義tabbar的方法創(chuàng)建按鈕
    
    [self.xTabBar setXTabBar];
    
//    NSArray *normalImageArr = @[@"tabbarZixunNormal",@"tabbarHomeNormal",@"tabbarMineNormal"];
//    NSArray *selectedImageArr = @[@"tabbarZixunSelected",@"tabbarHomeSelected",@"tabbarMineSelected"];
//    NSArray *titleArr = @[@"資訊",@"2",@"我的"];
//    
//    for (int i = 0; i < ChildControllerCount; i++) {
//        [self.xTabBar addBBIwithTitle:titleArr[i] NormalImageName:normalImageArr[i] SelectedImageName:selectedImageArr[i]];
//    }
    
    //  4、添加自定義tabbar,移除自帶tabbar
    [self.view addSubview:_xTabBar];
    [self.tabBar removeFromSuperview];
}

#pragma mark - XTabBar delegate Method
- (void)xTabbarClickBtnFrom:(NSInteger)from to:(NSInteger)to{
    self.selectedIndex = to;
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末戒祠,一起剝皮案震驚了整個(gè)濱河市骇两,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌姜盈,老刑警劉巖低千,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異馏颂,居然都是意外死亡示血,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門救拉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來矾芙,“玉大人,你說我怎么就攤上這事近上。” “怎么了拂铡?”我有些...
    開封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵壹无,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我感帅,道長(zhǎng)斗锭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任失球,我火速辦了婚禮岖是,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘实苞。我一直安慰自己豺撑,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開白布黔牵。 她就那樣靜靜地躺著聪轿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪猾浦。 梳的紋絲不亂的頭發(fā)上陆错,一...
    開封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音金赦,去河邊找鬼音瓷。 笑死,一個(gè)胖子當(dāng)著我的面吹牛夹抗,可吹牛的內(nèi)容都是我干的绳慎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼偷线!你這毒婦竟也來了磨确?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤声邦,失蹤者是張志新(化名)和其女友劉穎乏奥,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體亥曹,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡邓了,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了媳瞪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片骗炉。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蛇受,靈堂內(nèi)的尸體忽然破棺而出句葵,到底是詐尸還是另有隱情,我是刑警寧澤兢仰,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布乍丈,位于F島的核電站,受9級(jí)特大地震影響把将,放射性物質(zhì)發(fā)生泄漏轻专。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一察蹲、第九天 我趴在偏房一處隱蔽的房頂上張望请垛。 院中可真熱鬧,春花似錦洽议、人聲如沸宗收。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽镜雨。三九已至,卻和暖如春儿捧,著一層夾襖步出監(jiān)牢的瞬間荚坞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來泰國打工菲盾, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留颓影,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓懒鉴,卻偏偏與公主長(zhǎng)得像诡挂,于是被迫代替她去往敵國和親碎浇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

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

  • 在iOS原生的tabBar中璃俗,能夠?qū)崿F(xiàn)按鈕的點(diǎn)擊事件奴璃,能夠?qū)崿F(xiàn)視圖控制器的切換等,但是在實(shí)際工程中城豁,對(duì)于tabBa...
    請(qǐng)輸入賬號(hào)名閱讀 22,199評(píng)論 9 11
  • ·>本文原創(chuàng)苟穆,在此記錄一下自定義TabBar的幾種方式,有適用于微博唱星、QQ空間之類的簡(jiǎn)單方法雳旅;也有適用喜馬拉雅,閑...
    peaktan閱讀 1,765評(píng)論 3 11
  • 下面是自定義tabBar的一種效果, 如果有錯(cuò)誤請(qǐng)留言, 我會(huì)修改 一间聊、目標(biāo) 目標(biāo)效果目標(biāo)效果 tabBar中間添...
    伯wen閱讀 5,202評(píng)論 0 8
  • 代碼:參考黑馬明杰的微博項(xiàng)目中的自定義bar攒盈,加入一點(diǎn)自己的思路 一、思路講解 自定義tabbar替換定義的tab...
    huangxiongbiao閱讀 4,117評(píng)論 3 11
  • 圖文/完美的補(bǔ)丁 (大赤城白天的美景!) (大赤城夜景!) (大赤城遠(yuǎn)景!) 今年...
    完美的補(bǔ)丁閱讀 663評(píng)論 6 1