2017-11-03 7 views
1

ここで少し空白に主演されましたが、おそらく長すぎるので、ここで投稿しようとしています。ここでUITableViewHeaderFooterViewの制約はUIViewAlertForUnsatisfiableConstraintsになります

は、私が見ていますエラーです:

2017-11-02 22:43:23.972361-0700 TableViewCellHeaderViewLayoutTest[88247:17250641] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60c000092930 V:|-(12)-[UILabel:0x7fd450c0eb80'Header 1'] (active, names: '|':_UITableViewHeaderFooterContentView:0x7fd450c0f290)>", 
    "<NSLayoutConstraint:0x60c0000927f0 UILabel:0x7fd450c0eb80'Header 1'.bottom == _UITableViewHeaderFooterContentView:0x7fd450c0f290.bottom - 6 (active)>", 
    "<NSLayoutConstraint:0x60c000092ac0 'UIView-Encapsulated-Layout-Height' _UITableViewHeaderFooterContentView:0x7fd450c0f290.height == 17.5 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c0000927f0 UILabel:0x7fd450c0eb80'Header 1'.bottom == _UITableViewHeaderFooterContentView:0x7fd450c0f290.bottom - 6 (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

それは私のUITableViewHeaderFooterViewサブクラスの超簡単な実装です:私に際立っ

class TableViewSectionHeaderView: UITableViewHeaderFooterView { 

    let titleLabel = UILabel() 

    override init(reuseIdentifier: String?) { 
     super.init(reuseIdentifier: reuseIdentifier) 

     contentView.backgroundColor = .gray 

     titleLabel.backgroundColor = contentView.backgroundColor 
     titleLabel.font = UIFont.preferredFont(forTextStyle: .footnote) 

     contentView.addSubview(titleLabel) 

     titleLabel.translatesAutoresizingMaskIntoConstraints = false 
     titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 12).isActive = true 
     titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 6).isActive = true 
     titleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -6).isActive = true 
     titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -6).isActive = true 
    } 

} 

事は、この制約である:

"<NSLayoutConstraint:0x60c000092ac0 'UIView-Encapsulated-Layout-Height' _UITableViewHeaderFooterContentView:0x7fd450c0f290.height == 17.5 (active)>" 

私が調べると、priority1000、なぜ私自身の制約が失敗するように見えるかを説明します。

すべてが視覚的にうまくいきますが、私はその警告を心配しています。私は間違って何をしていますか?

+0

ラベルの高さが動的テキストによって変更された場合にヘッダービューを拡大させたいと仮定して、高さを自動的に設定することによって、headerViewのサイズを変更できるようにする必要があると思います。 – Daniel

+0

OMG @Danielありがとう!私は狂っていた。私が設定する必要があったのは 'sectionHeaderHeight'から' UITableViewAutomaticDimension'と 'estimatedSectionHeaderHeight'でした。 *ヘッドデスク* – runmad

+1

あなたが答えとして追加するなら、私は受け入れることができます☺ – runmad

答えて

1

セクションヘッダーの高さが実際のラベルの本来の高さとともに、ラベルの上部と下部の制約と競合しているように見えます。セクションヘッダーは、ラベルの高さによっては拡大または縮小する必要がありますが、厳密なヘッダーの高さの制約が原因です。

sectionHeaderHeightとestimatedSectionHeaderHeightをUITableViewAutomaticDimensionに設定すると、セクションの内容(この場合はラベル)に合わせて高さを調整できます。

関連する問題