一個方法搞定自定義tabBarItem中的控制元件位置均分
阿新 • • 發佈:2019-01-11
京東 qq 自定義tabbar控制元件均分
- 程式碼如下
//ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
[self setLablesWithArrOfTitle:@[@"one",@"哈哈",@"你妹的",@"我去"] andLeftDistance:15 andItHeight:21 andYcoordinate:255 andDestiView:self.view];
[self setLablesWithArrOfTitle :@[@"one",@"哈哈",@"你妹的",@"我去"] andLeftDistance:15 andItWidth:44 andItHeight:21 andYcoordinate:155 andDestiView:self.view];
}
對應方法
- 為了對比明顯將控制器背景色,以及控制元件背景色都設定上了不同的顏色以便於觀看
自動計算label的寬度
/**
* 設定中間部分控制元件兩兩之間距離相等,兩端控制元件距離兩側為中間控制元件的一半
* 根據lDis計算label寬度
* @param arrTitles label的名字陣列
* @param lDistance 最左邊控制元件距離螢幕左邊的距離
* @param height 控制元件高度
* @param y 控制元件y座標
*/
-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView{
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat margin = 2 * lDistance;
CGFloat width = (screenWidth - arrTitles.count * 2 * lDistance)/arrTitles.count;
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 whiteColor];
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.textAlignment = NSTextAlignmentCenter;
lbl.backgroundColor = [UIColor whiteColor];
lbl.text = arrTitles[i];
[destiView addSubview:lbl];
tempLabel = lbl;
}
}
自定義label的寬度
/**
* 設定中間部分控制元件兩兩之間距離相等
* 自定義label寬度
* @param arrTitles label的名字陣列
* @param lDistance 最左邊控制元件距離螢幕左邊的距離
* @param width 控制元件寬度
* @param height 控制元件高度
* @param y 控制元件y座標
*/
-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItWidth:(CGFloat)width andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView{
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat margin = (screenWidth - 2*lDistance - width*arrTitles.count) / (arrTitles.count - 1);
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 whiteColor];
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 whiteColor];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.text = arrTitles[i];
[destiView addSubview:lbl];
tempLabel = lbl;
}
}
最終效果
- 第一行是自定義控制元件寬度
- 第二行是自動計算控制元件寬度 根據自己需求呼叫即可
- 方法內部稍加修改就可以作為建立UIButton,UIImageView,UITxtField等等.