서브 클래 싱 된 UITableViewCell로 작업 중이며 자동 레이아웃을 사용하는 동안 UILabel의 텍스트를 모두 왼쪽 위 정렬해야합니다. 그 sizeToFit 실제로 자동 레이아웃 함께 사용해서는 안 읽고 그것을 피하고 싶습니다, 그리고 어떻게 든 제약 조건을 사용하고 싶습니다. 기본적으로 레이블의 텍스트는 셀을 재사용 할 때마다 다시 설정되므로 크기 조정은 재사용시 동적이어야합니다.UITableViewCell 셀 재사용 및 UILabel 자동 레이아웃 동적 크기 조정
이- (UILabel *)commentsLabel {
if (_commentsLabel == nil) {
_commentsLabel = [[UILabel alloc] init];
[_commentsLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
_commentsLabel.numberOfLines = 0;
_commentsLabel.lineBreakMode = NSLineBreakByWordWrapping;
_commentsLabel.preferredMaxLayoutWidth = 100;
}
return _commentsLabel;
}
라벨에 설정되는 자동 레이아웃 제약이 있습니다 (commentsLabel은 서브 뷰 셀의 서브 클래스에 self.customView에 추가됩니다) :
여기에 서브 클래 싱 된 세포 내부의 게으른 초기화 라벨입니다
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-locationToTopPadding-[locationLabel(locationHeight)]-locationToCommentsPadding-[commentsLabel]-commentsToBottomPadding-|"
options:0
metrics:@{
@"locationToTopPadding":@(locationToTopPadding),
@"locationHeight":@(locationHeight),
@"locationToCommentsPadding":@(locationToCommentsPadding),
@"commentsToBottomPadding":@(commentsToBottomPadding)
}
views:viewsDictionary]];
[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[thumbnailImageView]-[commentsLabel]-|"
options:0
metrics:@{@"commentsWidth":@(commentsWidth)}
views:viewsDictionary]];
단지 설정 :
self.commentsLabel.preferredMaxLayoutWidth = 100;
이 althoug를 작동하지 않는 것 h는 대부분의 답에서 언급됩니다.
에는 현재 구현되었지만 결과가 표시되지 않습니다. UILabel sizeToFit doesn't work with autolayout ios6
다른 답변을 시도했습니다. UILabel sizeToFit only works with AutoLayout turned off
는 난 그냥 위의 제약 그러나 나는 일을하거나 예외를 발생하지 않는 추가하려고 프로그래밍 제약 이외에 추가 할 수 있습니다 (1 개) 제약 조건을 누락 생각합니다. 코드에서 완전히 xibs가 없습니다.
ETA : 기존의 수직 구속 라인 내에서 높이 제약 조건을 설정했습니다 : 같은
여기 제안 : Dynamically change UILabel width not work with autolayout
그것을 만드는 위의 수직 구속 라인 :
-[commentsLabel(>[email protected])]-
내가했습니다 높이 값과 우선 순위 값으로 어지럽 혀 아무것도 바뀌지 않았습니다.
는ETA : 이벤트에
->=commentsToBottomPadding-
이 게시물을 본 적이 있습니까? http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – smileyborg