2014-12-23 2 views
0
내 사용자 지정 셀에 동적 높이의 UILabel의가있다

와 UILable의 높이를 설정, 높이 값은 방법을 통해 계산된다 :수동 자동 레이아웃

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

하지만, 문제는 내가 간단한 세트 label.frame = newFrame 때문에 자동 레이아웃의 내용에 맞게 할 수 없다는 것입니다.

그리고 스토리 보드의 요소에 많은 제약 조건을 설정하고 싶지 않습니다. 내가 정말로 필요한 것은 lable.constraint.height = newHeight과 같은 단 한 줄의 코드 일뿐입니다.

답변

1

yourlabel에 제약 조건을 설정하십시오.

[yourlabel setNeedsUpdateConstraints]; 

[yourlabel setTranslatesAutoresizingMaskIntoConstraints:NO]; 

NSLayoutConstraint *constraintHeight = [NSLayoutConstraint constraintWithItem:yourlabel 
                     attribute:NSLayoutAttributeHeight 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:nil 
                     attribute:NSLayoutAttributeNotAnAttribute 
                     multiplier:1.0 
                     constant:newheight]; 

[yourlabel addConstraints:@[constraintHeight]]; 
+0

나를 위해 작동하지 않습니다, 새로운 제약 조건을 추가 할 수없는 것 같습니다. BTW, 셀 구성 작업을 할 때이 코드를 추가합니다. – Allen

+0

맞춤 셀 클래스에 추가해보세요. –