iOS - 程序设计之完全自定义tabBarController

首先创建UINavigationController的基类HBNavigationController

  • 在基类中实现方法.
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if (self.childViewControllers.count==1) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}
  • 实现此方法目的: 当你点击界面进入二级子控制器的时候会自动隐藏tabbar

创建tabbarController基类HBTabbarController

  • 实现方法,通过kvc方式将系统的tabbar替换为自己自定义的tabbar[self setValue:hbTabBar forKey:@"tabBar"];
- (void)viewDidLoad {
- 
    [super viewDidLoad];
    HBTabBar * hbTabBar = [[HBTabBar alloc] initWithFrame:KFrame(0, KScreen_Height - 64, KScreen_Width, 64)];
    [self setValue:hbTabBar forKey:@"tabBar"];
}

创建tabBar子类HBTabBar继承UITabBar

  • 在内部实现构造方法
-(instancetype)initWithFrame:(CGRect)frame{

    if([super initWithFrame:frame]){
        UIView * tabBar = [[UIView alloc] init];
        tabBar.frame = self.bounds;
        _tabBar = tabBar;
        //将自定义的tabBar添加到系统tabbar的上面
        [self addSubview:tabBar];
    }
    return self;
}
  • tabBar初始化时会调用系统的方法- (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated
  • 重写此方法 == 添加对应的tabBarButtonItems到自定义的tabbar中
    • 创建label也就是标题
    • 创建button
- (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated{

    //创建lable以及通过设置lable的属性实现label在自定义的tabbar中的布局
    [self setLablesWithArrOfTitle:_arrTitle andLeftDistance:_lbl_leftDis andItHeight:_lbl_height andYcoordinate:_Lbl_YCoordinate andDestiView:self];
    //创建与lable对应的button实现上下对应
    [self setButtonsWithWidth:_btn_Width andHeiht:_btn_Height andYCoordinate:_Btn_YCoordinate andDestView:self];
}
  • 实现创建lable以及button的方法

/**
 创建tabBarItems

 @param width     宽度
 @param height    高
 @param y         y坐标
 @param destiView 目标view
 */
-(void)setButtonsWithWidth:(CGFloat)width andHeiht:(CGFloat)height andYCoordinate:(CGFloat) y andDestView:(UIView *)destiView{

    NSInteger count = _itemsCount;
    CGFloat lDis = (0.5*_lbl_width + _lbl_leftDis - _btn_Width * 0.5);
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat margin = (screenWidth - 2*lDis - width * count) / (count - 1);
    UIButton * tempBtn = [UIButton new];

    for (int i = 0; i< count; i++) {

        if (i == 0) {

            UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(lDis, y, width, height)];
            self.tempBtn = btn;
            [btn setBackgroundImage:self.btnImageSelected[i] forState:UIControlStateNormal];
            btn.tag = i;
            [destiView addSubview:btn];
            tempBtn = btn;
            [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
            continue;
        }
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((CGRectGetMaxX(tempBtn.frame)+margin), y, width, height);
        [btn setBackgroundImage:self.btnImageNormal[i] forState:UIControlStateNormal];
        [destiView addSubview:btn];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        tempBtn = btn;
    }
}

/**
 创建tabBarItems下面的label

 @param arrTitles 标题数组
 @param lDistance 距离左侧距离
 @param height    高
 @param y         y坐标
 @param destiView 目标view
 */
-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView{

    _itemsCount = arrTitles.count;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat margin = 2 * lDistance;
    CGFloat width = (screenWidth - 2 * lDistance - 2 * lDistance * (arrTitles.count - 1))/arrTitles.count;
    _lbl_width = width;
    UILabel * tempLabel = [UILabel new];

    for (int i = 0; i< arrTitles.count; i++) {

        if (i == 0) {

            UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(lDistance, y, width, height)];
            lbl.backgroundColor = [UIColor blueColor];
            self.tempLbl = lbl;
            lbl.textColor = self.lblColorSelected;
            [self.arrLabel addObject:lbl];
            lbl.font = [UIFont systemFontOfSize:_lbl_font];
            lbl.textAlignment = NSTextAlignmentCenter;
            lbl.text = arrTitles[0];
            [destiView addSubview:lbl];
            tempLabel = lbl;
            continue;
        }
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), y, width, height)];
        lbl.backgroundColor = [UIColor blueColor];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.text = arrTitles[i];
        lbl.textColor = self.lblColorNormal;
        [self.arrLabel addObject:lbl];
        lbl.font = [UIFont systemFontOfSize:_lbl_font];
        [destiView addSubview:lbl];
        tempLabel = lbl;
    }
}
  • 在.h文件中添加tabbar所有的属性,以便于修改定义tabbar各种属性
/* 点击tabbaritem回调 */
@property(nonatomic,copy) void(^callBack)(NSInteger index);
/* label字体大小 */
@property (nonatomic, assign) NSInteger lbl_font;
/* label距离左侧距离 */
@property (nonatomic, assign) CGFloat lbl_leftDis;
/* lable高度 */
@property (nonatomic, assign) CGFloat lbl_height;
/* lable的y坐标 */
@property (nonatomic, assign) CGFloat Lbl_YCoordinate;

/* btn宽度 */
@property (nonatomic, assign) CGFloat btn_Width;
/* btn高度 */
@property (nonatomic, assign) CGFloat btn_Height;
/* y坐标 */
@property (nonatomic, assign) CGFloat Btn_YCoordinate;
/* 未选中状态下的btn图片数组 */
@property (strong, nonatomic) NSArray * btnImageNormal;
/* 选中状态下的btn图片数组 */
@property (strong, nonatomic) NSArray * btnImageSelected;
/* 未选中状态下lable字体颜色 */
@property (strong, nonatomic) UIColor * lblColorNormal;
/* 选中状态下label字体颜色 */
@property (strong, nonatomic) UIColor * lblColorSelected;
/* label的内容数组 */
@property (strong, nonatomic) NSArray * arrTitle;
/* tabbar的背景色 */
@property (strong, nonatomic) UIColor * tabBarBackgroundColor;

代码地址 : https://github.com/adampei/diyTabBar

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值