この問題の解決策を一日中検索しました。私はtableViewHeaderView
の中のテキストの長さに基づいて私自身のUILabelsを自動化しようとしています。通常、UIView内の私のUILabelsで、私はUILabelにトップ、リーディング、およびトレーリングの制約を設定し、私が望むように動作します。しかし、私はtableViewHeaderView
の内部で働くことができません。私はトップとリーディングの制約を設定することができますが、私の後続の制約は機能していないようです。テキストは画面の幅を超えます。テーブルビューのheaderView内で複数行のUILabelに自動レイアウト制約を追加するにはどうすればよいですか?
preferredMaxLayoutWidth
プロパティを数値に設定すると問題は解決しますが、ハードコードする必要はありません。
私が間違っている場合は私を修正しますが、先頭と末尾の制約を設定すると、ビューの幅がわかりません。それで、私はその値でpreferredMaxLayoutWidth
を設定することができました。しかし、その値は画面の幅よりも長い2403です。
他にも誰かがこれを体験しますか?
#import "CustomTableViewHeader.h"
@implementation ReplyHeader{
UILabel *questionLabel;
}
questionLabel = [[UILabel alloc]init];
questionLabel.text = @"SAMPLE TEXT: I had a question about how I can be a better human being using your method. I belive it is an integral part of what it means to be a human so I want to learn more if you are able give more details about it. I also found that what you said about the dogs out there is very cool and would love to learn more about that.";
questionLabel.font = [UIFont fontWithName:@"AvenirNext-Regular" size:17];
questionLabel.lineBreakMode = NSLineBreakByWordWrapping;
questionLabel.numberOfLines = 0;
questionLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:questionLabel];
制約
[self addConstraint:[NSLayoutConstraint constraintWithItem:questionLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:questionLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:questionLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:10]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:questionLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-10]];
- (void)viewDidLayoutSubviews
{
questionLabel.preferredMaxLayoutWidth = questionLabel.frame.size.width;
[self layoutIfNeeded];
}
のようなものを試してみてください、あなたは '' awakeFromNib'内部questionLabel'を追加していますか?そして、あなたはどこに制約を加えていますか?そして 'UITableView'の制約は何ですか? – Rikh