2016-04-25 43 views
-1

我有兩個標籤,添加約束標籤編程

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 40)]; 
[label1 setBackgroundColor:[UIColor clearColor]]; 
[label1 setText:@"label1"]; 
[[self view] addSubview:label1]; 


// Create Label 
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 40)]; 
[label2 setBackgroundColor:[UIColor clearColor]]; 
[label2 setText:@"label2"]; 
[[self view] addSubview:label2]; 

我想通過程序生成這兩個標籤之間的空間,但我不確定如何做到這一點。我試過使用一些來自stackoverflow的答案,但他們給了我錯誤。

像這樣的:

NSLayoutConstraint *c1 = [NSLayoutConstraint 
          constraintWithItem:UIl.label2 
          attribute:NSLayoutAttributeLeading 
          relatedBy:NSLayoutRelationEqual 
          toItem:self 
          attribute:NSLayoutAttributeLeading 
          multiplier:1.0f 
          constant:8.0f]; 
+0

它給出了什麼錯誤?另外我建議使用砌體進行自動佈局。 –

回答

0

我建議用磚石爲應用使用磚石programmatically.By限制,你可以做簡單:

Step1-約束標籤1

[label1 mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(self.view.mas_top); 
     make.left.equalTo(self.view.mas_left); 
    }]; 

第2步 - 限制標籤2

[label2 mas_makeConstraints:^(MASConstraintMaker *make) { 
      make.top.equalTo(label1.mas_top); 
      make.left.equalTo(label1.mas_left).with.offset(yourValue); 
     }];