1. 程式人生 > >程式設計之完全自定義tabBarController

程式設計之完全自定義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;