2014-08-29 19 views
1

我試圖使用通常的刪除標籤並使用自動佈局調整所有內容的方法。 How to change label constraints during runtime。但是,我正在使用UITableView 標頭當在UITableView標題中將標籤約束設置爲0時,Autolay失敗

UITableView header

當我設定的標籤之一的高度約束:

self.labelAHeight.constant = 0; 

結果是:

Unable to simultaneously satisfy constraints. 
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) 
look at each constraint and try to figure out which you don't expect; (2) find the code that added 
the unwanted constraint or constraints and fix it. (Note: If you're seeing 
NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the 
UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0xb0faad0 V:[UILabel:0xb0fa830(0)]>", 
    "<NSLayoutConstraint:0xb0772a0 V:[UILabel:0xb077300(58)]>", 
    "<NSLayoutConstraint:0xb076cc0 V:|-(0)-[UILabel:0xb0fa830] (Names: '|':UIView:0xb077660)>", 
    "<NSLayoutConstraint:0xb076c90 V:[UILabel:0xb077300]-(0)-| (Names: '|':UIView:0xb077660)>", 
    "<NSLayoutConstraint:0xb076c00 V:[UILabel:0xb0fa830]-(0)-[UILabel:0xb077300]>", 
    "<NSAutoresizingMaskLayoutConstraint:0xb06f030 h=--& v=--& V:[UIView:0xb077660(119)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xb0772a0 V:[UILabel:0xb077300(58)]> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> 
may also be helpful. 

我試圖[self.labelA setTranslatesAutoresizingMaskIntoConstraints:NO]這於事無補。我試過[self.table.tableHeaderView setTranslatesAutoresizingMaskIntoConstraints:NO]*** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794碰撞。也許,我在錯誤的地方做了這個(就在設置約束條件之前)。

似乎並不重要的約束變化發生在哪裏,例如, viewDidLoad或按鈕處理程序。

它看起來好像標題想要達到一定的高度(<NSAutoresizingMaskLayoutConstraint:0xb06f030 h=--& v=--& V:[UIView:0xb077660(119)]>根據其內容),如果內容改變,自動佈局變得非常不高興。

任何人都可以幫助調試或建議如何解決它? (我試過儀器,但是被添加和刪除的所有約束困惑......)

回答

0

以下是部分答案。可以防止上面的約束問題,但是,我還沒有找到如何使表的其餘部分隨着標題大小的變化而移動。

1)在故事板中設置表頭的translatesAutoresizingMaskIntoConstraints = NO。

enter image description here

2)現在在頭部設置的約束,所述自動尺寸調整掩模沒有貢獻的約束。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self.table addConstraint: 
      [NSLayoutConstraint constraintWithItem:self.table.tableHeaderView 
              attribute:NSLayoutAttributeWidth 
              relatedBy:NSLayoutRelationEqual 
              toItem:self.table 
              attribute:NSLayoutAttributeWidth 
             multiplier:1 
              constant:0]]; 
    [self.table addConstraint: 
      [NSLayoutConstraint constraintWithItem:self.table.tableHeaderView 
              attribute:NSLayoutAttributeTop 
              relatedBy:NSLayoutRelationEqual 
              toItem:self.table 
              attribute:NSLayoutAttributeTop 
             multiplier:1 
              constant:0]]; 
} 

標籤A的高度約束的常數設置爲零的結果是:

之前:

enter image description here

後(它可以看出,頂部標籤具有消失了,但表格行沒有移動到標題底部):

enter image description here

我已驗證標題視圖實際上已降低其高度。

我試過setNeedsLayoutsetNeedsDisplay的標題,表格,視圖。我已經嘗試beginUpdatesendUpdates圍繞設置的不變。我試過reloadData。但沒有任何工作。

我懷疑表頭和單元之間沒有約束。

如果有人有任何想法,我會很高興聽到他們。

EDIT

3)表可以由具有重繪以下:

self.labelAHeight.constant = 0; 
[self performSelector:@selector(reloadHeader) withObject:nil afterDelay:0]; 

// [self.table.tableHeaderView setNeedsUpdateConstraints]; 
//  
// [UIView animateWithDuration:0.3f animations:^{ 
//  [self.table.tableHeaderView layoutIfNeeded]; 
// }]; 

- (void) reloadHeader { 
// [self.table beginUpdates]; 
    self.table.tableHeaderView = self.table.tableHeaderView; 
// [self.table endUpdates]; 
} 

在取消上述嘗試的線條進行動畫處理的變化,但標題的動畫和表格的其餘部分並沒有嚴格地綁定在一起,因此標題和表格之間可能會出現空白即

還不完美,但越來越接近。