iOS UITableView设置tableHeaderView时发生约束错误 UIView-Encapsulated-Layout-Height UIView-Encapsulated-Layout-Width
在将UITableView的tableHeaderView设置为我自己创建的View的时候,
当我为这个自定义View添加约束之后启动调试,
然后符号断点UIViewAlertForUnsatisfiableConstraints命中了,
终端输出了如下的报错信息:
( "<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自动添加了其他约束,
然而它添加的约束和你添加的约束的优先级是相同的。
所以解决问题的关键就是将你设置的约束的优先级降低;
解决方案参考:
一、如果你使用了UIKit提供的API来设置约束,可以参考如下OC代码:
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];
二、如果你使用了SnapKit来设置约束,可以参考如下Swift代码:
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
Stay hungry,stay foolish.