来自 xib 的自定义 UIView 始终具有 600 pt 的高度(UIView-Encapsulated-Layout-Height 约束)
Custom UIView from xib always has height of 600 pt (UIView-Encapsulated-Layout-Height constraint)
我正在加载自定义 UIView(来自 xib 文件)作为 UITableView 的 header 视图:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return NSBundle.mainBundle().loadNibNamed("ActivityFeedHeaderView", owner: nil, options: nil)[0] as ActivityFeedHeaderView
}
但是无论我的自动布局约束如何,UIView 的高度始终为 600。所有子视图都有高度约束和垂直 space 约束,因此 UIView 的整体高度应由此定义。但是我遇到了这些约束冲突:
(
"<NSLayoutConstraint:0x7fae10c1d160 V:[UILabel:0x7fae10c39340'Something'(24)]>",
"<NSLayoutConstraint:0x7fae10c4bdc0 V:[UILabel:0x7fae10c6e850'Area 31'(17)]>",
"<NSLayoutConstraint:0x7fae10c59510 V:[UITableView:0x7fae11078400(308)]>",
"<NSLayoutConstraint:0x7fae10c276e0 V:|-(33)-[UILabel:0x7fae10c39340'Something'] (Names: '|':Fruitful_Reports.ActivityFeedHeaderView:0x7fae10c6ed00 )>",
"<NSLayoutConstraint:0x7fae10c27780 V:[UILabel:0x7fae10c39340'Something']-(8)-[UILabel:0x7fae10c6e850'Area 31']>",
"<NSLayoutConstraint:0x7fae10c27870 V:[UITableView:0x7fae11078400]-(20)-| (Names: '|':Fruitful_Reports.ActivityFeedHeaderView:0x7fae10c6ed00 )>",
"<NSLayoutConstraint:0x7fae10c278c0 V:[UILabel:0x7fae10c6e850'Area 31']-(24)-[UITableView:0x7fae11078400]>",
"<NSLayoutConstraint:0x7fae10f869d0 'UIView-Encapsulated-Layout-Height' V:[Fruitful_Reports.ActivityFeedHeaderView:0x7fae10c6ed00(600)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fae10c59510 V:[UITableView:0x7fae11078400(308)]>
我不想要的约束是:
"<NSLayoutConstraint:0x7fae10f869d0 'UIView-Encapsulated-Layout-Height' V:[Fruitful_Reports.ActivityFeedHeaderView:0x7fae10c6ed00(600)]>"
为什么系统添加了这个,有什么方法可以删除它吗?干杯
编辑:
添加此约束是因为 600 是我从此方法返回的值:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
headerView = (NSBundle.mainBundle().loadNibNamed("ActivityFeedHeaderView", owner: nil, options: nil)[0] as ActivityFeedHeaderView)
return headerView!.frame.size.height
}
但我仍然想知道,如何让 headerView 根据其子视图的高度和垂直 space 约束更新其高度?
问题是我正在实施:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
...
}
我删除了那个方法,并将这些设置添加到 UITableView,header 现在显示正确的高度:
self.tableView.estimatedSectionHeaderHeight = 300.0
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
我正在加载自定义 UIView(来自 xib 文件)作为 UITableView 的 header 视图:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return NSBundle.mainBundle().loadNibNamed("ActivityFeedHeaderView", owner: nil, options: nil)[0] as ActivityFeedHeaderView
}
但是无论我的自动布局约束如何,UIView 的高度始终为 600。所有子视图都有高度约束和垂直 space 约束,因此 UIView 的整体高度应由此定义。但是我遇到了这些约束冲突:
(
"<NSLayoutConstraint:0x7fae10c1d160 V:[UILabel:0x7fae10c39340'Something'(24)]>",
"<NSLayoutConstraint:0x7fae10c4bdc0 V:[UILabel:0x7fae10c6e850'Area 31'(17)]>",
"<NSLayoutConstraint:0x7fae10c59510 V:[UITableView:0x7fae11078400(308)]>",
"<NSLayoutConstraint:0x7fae10c276e0 V:|-(33)-[UILabel:0x7fae10c39340'Something'] (Names: '|':Fruitful_Reports.ActivityFeedHeaderView:0x7fae10c6ed00 )>",
"<NSLayoutConstraint:0x7fae10c27780 V:[UILabel:0x7fae10c39340'Something']-(8)-[UILabel:0x7fae10c6e850'Area 31']>",
"<NSLayoutConstraint:0x7fae10c27870 V:[UITableView:0x7fae11078400]-(20)-| (Names: '|':Fruitful_Reports.ActivityFeedHeaderView:0x7fae10c6ed00 )>",
"<NSLayoutConstraint:0x7fae10c278c0 V:[UILabel:0x7fae10c6e850'Area 31']-(24)-[UITableView:0x7fae11078400]>",
"<NSLayoutConstraint:0x7fae10f869d0 'UIView-Encapsulated-Layout-Height' V:[Fruitful_Reports.ActivityFeedHeaderView:0x7fae10c6ed00(600)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fae10c59510 V:[UITableView:0x7fae11078400(308)]>
我不想要的约束是:
"<NSLayoutConstraint:0x7fae10f869d0 'UIView-Encapsulated-Layout-Height' V:[Fruitful_Reports.ActivityFeedHeaderView:0x7fae10c6ed00(600)]>"
为什么系统添加了这个,有什么方法可以删除它吗?干杯
编辑:
添加此约束是因为 600 是我从此方法返回的值:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
headerView = (NSBundle.mainBundle().loadNibNamed("ActivityFeedHeaderView", owner: nil, options: nil)[0] as ActivityFeedHeaderView)
return headerView!.frame.size.height
}
但我仍然想知道,如何让 headerView 根据其子视图的高度和垂直 space 约束更新其高度?
问题是我正在实施:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
...
}
我删除了那个方法,并将这些设置添加到 UITableView,header 现在显示正确的高度:
self.tableView.estimatedSectionHeaderHeight = 300.0
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension