【iOS UI】自定义TabBar

自定义TabBar

一、XTabBarButton

XTabBarButton.h  

#import <UIKit/UIKit.h>

@interface XTabBarButton : UIButton

//  标签
@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 - 创建子控件
- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {
        
        //  1、创建标签
        _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、创建按钮
        _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>

//导入自定义的XTabBarButton
#import "XTabBarButton.h"  

//子控制器数量=3
#define ChildControllerCount 3

@class XTabBar;

//delegate
@protocol XTabBarDelegate <NSObject>

//delegate methods
//  点击XTabBarButton后,tabbarController执行该方法
- (void)xTabbarClickBtnFrom:(NSInteger)from to:(NSInteger)to;

@end


@interface XTabBar : UIView  //继承自UIView

@property (nonatomic, weak) id<XTabBarDelegate> delegate;    //代理
@property (nonatomic, strong) NSArray *tabBarTitleArr;    //自定义XTabBar上的标题数组
@property (nonatomic, strong) NSArray *tabBarNormalImgArr;    //自定义XTabBar上的正常状态图标数组
@property (nonatomic, strong) NSArray *tabBarSelectedImgArr;    //自定义XTabBar上的点击状态图标数组
@property (nonatomic, strong) NSMutableArray *btnArr;   //存放自定义按钮的可变数组
@property (nonatomic, strong) UIButton *selectedBtn;    //点击了的按钮

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

//  新方法设置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 - 懒加载存放按钮的可变数组
- (NSMutableArray *)btnArr{
    if (!_btnArr) {
        _btnArr = [[NSMutableArray alloc] init];
    }
    return _btnArr;
}

#pragma mark - 设置子控件的位置
- (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;    //
    }
    
    //  点击第二个按钮
    if (_btnArr.count == 3) {

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

        //  2、把中间按钮传给点击方法,使tabBar控制器直接显示中间的控制器
        [self xTabBarButtonClickWithBtn:fistPressBtn];
    }
}

#pragma mark - 添加一个自定义的按钮
- (void)addBBIwithTitle:(NSString *)title NormalImageName:(NSString *)normalImage SelectedImageName:(NSString *)selectedImageName{
    
    //  1、创建自定义的按钮
    XTabBarButton *xTabBarButton = [[XTabBarButton alloc] init];
    
    //  2、自定义按钮的子按钮与标签不可交互,设置label的title
    xTabBarButton.btn.userInteractionEnabled = NO;
    xTabBarButton.label.userInteractionEnabled = NO;
    xTabBarButton.label.text = title;
    
    //  3、自定义按钮的子按钮设置图片
    [xTabBarButton.btn setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];

    //  4、自定义按钮绑定方法
    [xTabBarButton addTarget:self action:@selector(xTabBarButtonClickWithBtn:) forControlEvents:UIControlEventTouchDown];
    
    //  5、没有title时
    if (title.length == 0) {
        //  隐藏label
        xTabBarButton.label.alpha = 0;
        
        //  调整子按钮的大小
        xTabBarButton.btn.frame = CGRectMake(5, 5, 5, 5);
        
    }
    
    
    //  6、添加自定义的xTabbarButton到自定义tabBar上
    [self addSubview:xTabBarButton];
    [self.btnArr addObject:xTabBarButton];
    
    
}


//  新方法设置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、循环创建按钮
        XTabBarButton *xTabBarButton = [XTabBarButton new];
        
        //  2、xTabBarButton的子按钮与标签不可交互
        xTabBarButton.btn.userInteractionEnabled = NO;
        xTabBarButton.label.userInteractionEnabled = NO;
        
        //  3、设置标签文字与子按钮图片
        xTabBarButton.label.text = _tabBarTitleArr[i];
        
        //  3.1、当没有title时,调整按钮的位置
        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、添加到自定义按钮的数组

        [self.btnArr addObject:xTabBarButton];
        
        //  7、添加创建好的按钮
        [self addSubview:xTabBarButton];
     
    }
}

#pragma mark - 点击自定义tabbar中的xTabbarButton
- (void)xTabBarButtonClickWithBtn:(UIButton *)btn{
    
    //  1、遍历按钮数组中的按钮,改变所有按钮的图片和标签颜色
    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、设置当前按钮的照片和标签颜色,调用代理方法
    XTabBarButton *preBtn = [btn viewWithTag:btn.tag];
    preBtn.label.textColor = [UIColor orangeColor];
    [preBtn.btn setImage:[UIImage imageNamed:_tabBarSelectedImgArr[btn.tag]] forState:UIControlStateNormal];
    
    //  3、调用代理方法
    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"

//子控制器个数 3个
#define ChildControllerCount 3

@interface XTabBarController ()<XTabBarDelegate>

@end

@implementation XTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  1、初始化自定义的tabbar,设置委托
    _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的方法创建按钮
    
    [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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容