iOS UITableView設置tableHeaderView時發生約束錯誤 UIView-Encapsulated-Layout-Height UIView-Encapsulated-Layout

 

 

在將UITableViewtableHeaderView設置爲我本身建立的View的時候,html

當我爲這個自定義View添加約束以後啓動調試,swift

而後符號斷點UIViewAlertForUnsatisfiableConstraints命中了,spa

終端輸出了以下的報錯信息:.net

(
"<SnapKit.LayoutConstraint:0x1702a64e0@MAskDetailVC.swift#120 UIView:0x1017eb5e0.height == 211.666666666667>",
"<NSLayoutConstraint:0x170682760 'UIView-Encapsulated-Layout-Height' UIView:0x1017eb5e0.height == 212 (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170682760 'UIView-Encapsulated-Layout-Height' UIView:0x1017eb5e0.height == 212 (active)>

  

 

這實際上是CollectionView爲你的自定義View自動添加了其餘約束調試

然而它添加的約束你添加的約束優先級是相同的。
orm

 

因此解決問題的關鍵就是將你設置的約束的優先級下降htm

 

 

 

解決方案參考:blog

 

1、若是你使用了UIKit提供的API來設置約束,能夠參考以下OC代碼:get

NSArray * collectionConstraints_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_collectionview]-0-|" options:0 metrics:nil views:viewsDic];
[self addConstraints:collectionConstraints_V];

NSArray * collectionConstraints_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-103-[_collectionview]" options:0 metrics:nil views:viewsDic];
[self addConstraints:collectionConstraints_H];

NSLayoutConstraint * constraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_collectionview]-0-|" options:0 metrics:nil views:viewsDic].firstObject;
constraint.priority = UILayoutPriorityDefaultLow;

[self addConstraint:constraint];

  

 

2、若是你使用了SnapKit來設置約束,能夠參考以下Swift代碼:it

askContainer.snp.makeConstraints { (m) in
    m.top.equalTo(detailTV.snp.top)
    m.leading.equalTo(detailTV.snp.leading)
    m.width.equalTo(SCREEN_WIDTH)
    let height = m.height.equalTo(height).constraint
    height.layoutConstraints.first?.priority = UILayoutPriorityDefaultLow
}

  

 

 

參考資料:http://blog.csdn.net/hello_hwc/article/details/47129711

 

 


轉載請註明出處:http://www.cnblogs.com/ficow/p/7250330.html

相關文章
相關標籤/搜索