2015-04-06 65 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