私はサブクラス化された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