一個view中多個lable(或其他控件)中間的每個lable距離左右lable都相等,代碼設置frame約束

  • 一個view鐘多個一樣的空間例如lable,txtField,或者其他控件一樣的控件,屏幕最兩邊的控件距離屏幕距離一定,中間的控件兩兩之間的距離相等.通過一個簡單的封裝方法來搞定.
  • 之前用Masonry來約束,雖然效果得到了,但是還是有警告,看著不爽啊,所以干脆直接計算fram進行約束.也不難
  • 具體方法如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

-(void)setUI{
    //下面注釋的方法跟最下面封裝的方法最終效果一樣
//    CGFloat leftDistance = 15.0; //最左邊控件距離屏幕左邊的距離
//    CGFloat lblWidth = 40.0; //控件寬度
//    CGFloat lblHeight = 21.0; //控件高度,控件y坐標我設置成了20
//    
//    CGFloat margin = (kScreenW - 30 - 160) / 3.0;
//    UILabel * lblOne = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 40, 21)];
//    lblOne.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:lblOne];
//    lblOne.text = @"one";
//    
//    UILabel * lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lblOne.frame)+margin, 20, 40, 21)];
//    lblTwo.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:lblTwo];
//    lblTwo.text = @"two";
//    
//    UILabel * lblThree = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lblTwo.frame)+margin, 20, 40, 21)];
//    lblThree.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:lblThree];
//    lblThree.text = @"three";
//
//    UILabel * lblFour = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lblThree.frame)+margin, 20, 40, 21)];
//    lblFour.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:lblFour];
//    lblFour.text = @"four";
    
    [self setLablesWithArrOfTitle:@[@"一",@"二",@"三",@"四"] andLeftDistance:15.0 andItWidth:40 andItHeight:21 andYcoordinate:20];
}

/**
 *  設置中間部分控件兩兩之間距離相等
 *
 *  @param arrTitles label的名字數(shù)組
 *  @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{
//獲取屏幕寬度
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
//計算中間兩個lable之間的距離
    CGFloat margin = (screenWidth - 2*lDistance - width*arrTitles.count) / (arrTitles.count - 1);
//緩存tempLable用于存儲當前創(chuàng)建出來的lable,目的是當創(chuàng)建下一個lable的時候通過CGRectGetMaxX(tempLabel.frame)這個方法來獲取上一個lable的最大x坐標maxX加上margin就得到了下一個lable的x坐標,以此類推即可.
    UILabel * tempLabel = [UILabel new];
    
    for (int i = 0; i< arrTitles.count; i++) {
//由于第一個lable比較特殊,他距離屏幕左邊與右邊的lable距離不相等,所以在此進行判斷過濾.
        if (i == 0) {
            
            UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(lDistance, y, width, height)];
            lbl.textAlignment = NSTextAlignmentCenter;
            lbl.text = arrTitles[0];
            [self.view addSubview:lbl];
            tempLabel = lbl;
            continue;
        }
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), y, width, height)];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.text = arrTitles[i];
        [self.view addSubview:lbl];
        tempLabel = lbl;
    }
    
}

  • 最終的效果是這樣
    • '一'距離左邊15個點, '二'與'三'距離兩邊的lable都相等,'四'與'一'距離屏幕右邊與左邊都相等 = 15


      Paste_Image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容