2012-10-08 33 views
16

我想添加一個UIView到UITableView的標題,然後,使用NSLayoutConstraints我想給它一個高度。在UITableView的標題視圖上使用NSLayoutConstraints

我已經瀏覽了Apple Docs和WWDC 2012的視頻,但我無法在任何地方找到此特定錯誤!

我有以下代碼:

- (UIImageView *)logoImageView 
{ 
    if (!_logoImageView) { 
     _logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]]; 
     [_logoImageView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    } 
    return _logoImageView; 
} 

...在viewDidLoad中

UIView *tableHeaderView = [[UIView alloc] init]; 
tableHeaderView.translatesAutoresizingMaskIntoConstraints = NO; 

[tableHeaderView addSubview:self.logoImageView]; 

[self.tableView setTableHeaderView:tableHeaderView]; 

NSDictionary *constraintViews = @{@"tableView" : self.tableView, @"tableHeaderView" : tableHeaderView, @"logoImageView" : self.logoImageView}; 

[self.tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tableHeaderView(50)]" 
                     options:0 
                     metrics:nil 
                     views:constraintViews]]; 

然而,當我運行它,我得到以下錯誤:

2012-10-08 16:31:34.559 myApp[6934:c07] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776 
2012-10-08 16:31:34.561 myApp[6934:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.' 
*** First throw call stack: 
(0x199b012 0x17c0e7e 0x199ae78 0x1256f35 0x7589ef 0x17d46b0 0x622fc0 0x61733c 0x622eaf 0x7f78cd 0x7401a6 0x73ecbf 0x73ebd9 0x73de34 0x73dc6e 0x73ea29 0x741922 0x7ebfec 0x738bc4 0x738dbf 0x738f55 0x741f67 0x705fcc 0x706fab 0x718315 0x71924b 0x70acf8 0x27e8df9 0x27e8ad0 0x1910bf5 0x1910962 0x1941bb6 0x1940f44 0x1940e1b 0x7067da 0x70865c 0x6d5d 0x2395) 
libc++abi.dylib: terminate called throwing an exception 
+3

我可以問你是否曾經找到解決這個問題的辦法? –

回答

2

解決此問題的最佳方法是使用Interface Builder設置UITableView標題,然後將高度爲NSLayoutConstraint的出口添加到該高度。

+3

你可以詳細說明嗎?不知道這是什麼意思。 – jgvb

+0

'NSLayoutConstraint's是常規的對象,所以你可以爲它們創建出口從界面生成器,並在運行時操作它們的屬性,就像你爲一個IB視圖(例如) –

+0

我看不到一種方法來添加高度或寬度佈局約束到'UITableView'的頁眉或頁腳在故事板。添加新的約束條件'裏面的東西都被封鎖了。 – derpoliuk

3

我當我嘗試設置表頭視圖時有相同的異常。 當我發現視圖有Contraints時,我只是取消選中Storyboard中的「使用Autolayout」選項。

截圖:http://i.stack.imgur.com/5jWHy.png

這解決了我。

+8

這只是解決這個問題,而不是修復它:(你不能只關閉自動佈局! – jgvb

1

試試這個:

logoImageView.translatesAutoresizingMaskIntoConstraints = YES; 

(如果沒有工作,嘗試刪除:

tableHeaderView.translatesAutoresizingMaskIntoConstraints = NO; //Remove this line 

:)

0

我沒有得到這個問題的任何妥善的解決辦法,但你可以通過使用框架修復它,而不是將translatesAutoresizingMaskIntoConstraints屬性設置爲否(默認情況下它的是,所以不要設置它)

CGRect headerViewFrame = CGRectMake(0,0,320,60); //Frame for your header/footer view 

UIView *tableHeaderView = [[UIView alloc] initWithFrame:headerViewFrame]; 
tableHeaderView.translatesAutoresizingMaskIntoConstraints = Yes; //Or don't set it at all 
[self.tableView setTableHeaderView:tableHeaderView]; 
相關問題