2017-01-18 6 views
0

この問題の解決策を一日中検索しました。私は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]; 
} 
+0

のようなものを試してみてください、あなたは '' awakeFromNib'内部questionLabel'を追加していますか?そして、あなたはどこに制約を加えていますか?そして 'UITableView'の制約は何ですか? – Rikh

答えて

0

のUITableViewのは、自動レイアウトと仲良くしていない更新ビュー。ヘッダーとフッターの両方がその問題を継承しています。 すでにご存知のように、UITableViewsは派手なUIScrollViewです。スクロールビューで作業しようとすると、多くの要素やAutoLayoutがうまく動作しないことを知っておく必要があります。それは遅れる。そのため、UITableViewはセルを再利用します。パフォーマンスが向上し、AutoLayoutは使用されません。

あなたは

// with this you will get the most compressed size for your view if the constraints properly define it's size 
CGFloat height = [footer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 
footer = CGRectMake(CGRectGetMinX(view.frame), CGRectGetMinY(footer.frame), CGRectGetWidth(footer.bounds), height); 
// This will tell the layout engine to ignore the view and not try to resize it 
view.autoresizingMask = UIViewAutoresizingNone; 
self.tableView.tableFooterView = footer; 
関連する問題