2015-04-06 64 views
0

我加载一个自定义的UIView(从XIB文件)作为一个UITableView的标题视图的高度:自定义的UIView总是有600磅(UIView的封装,布局,高度约束)

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    return NSBundle.mainBundle().loadNibNamed("ActivityFeedHeaderView", owner: nil, options: nil)[0] as ActivityFeedHeaderView 
} 

但无论我的自动布局约束如何,UIView的高度始终为600.所有子视图都有高度约束和垂直空间约束,因此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根据子视图的高度和垂直空间限制来更新高度?

+0

什么是您对超视图设置的视图的限制是什么? – 2015-04-06 10:03:11

+0

我没有添加任何关于superview的约束,因为我将视图交给系统(即我没有将它添加到任何superview自己)。我应该向superview添加约束吗? – Michael 2015-04-06 10:16:06

+0

我更新了帖子,提供了更多关于额外约束来自何处的信息。 – Michael 2015-04-06 10:16:40

回答

0

的问题是,我正在实施:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    ... 
} 

我删除的方法,并添加这些设置到的UITableView,标题现在正确的高度显示:

self.tableView.estimatedSectionHeaderHeight = 300.0 
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension