2013-09-24 89 views
9

以前在iOS6中,我的聊天消息視圖控制器顯示並在IB中仔細構建的自動佈局約束正確動畫。下面是用在Xcode 5刪除約束當前視圖層次結構:iOS7問題與接口生成器生成的NSIBPrototypingLayoutConstraint自動佈局約束

IB layout

升級到的XCode 5和iOS7後,我發現我需要清除,以便所有的舊約束佔默認半透明的狀態條,否則我的工具欄會在狀態欄下。儘管調用了setEdgesForExtendedLayout:使用UIRectEdgeNone,但發生了這種情況。

我現在嘗試在viewDidLoad中編程創建所有的約束:

// self.view.translatesAutoresizingMaskIntoConstraints = NO; // errors either way 
NSArray *viewHorizConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_toolbar]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_toolbar)]; 
[self.view addConstraints:viewHorizConstraints]; 
viewHorizConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_viewContainer]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_viewContainer)]; 
[self.view addConstraints:viewHorizConstraints]; 

NSArray *viewVertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(20)-[_toolbar(44)]-[_viewContainer]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_toolbar, _viewContainer)]; 
[self.view addConstraints:viewVertConstraints]; 

NSArray *tableContainerVertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tableView(<[email protected])][_viewInputContainer(44)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView, _viewInputContainer)]; 
[_viewTableContainer addConstraints:tableContainerVertConstraints]; 


NSArray *containerVertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_viewTableContainer][_viewOptions]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_viewTableContainer, _viewOptions)]; 
[_viewContainer addConstraints:containerVertConstraints]; 

// _constraintContainerVertSpace = [NSLayoutConstraint constraintWithItem:_viewTableContainer attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_viewContainer attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]; // same as below 

containerVertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[_viewTableContainer]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_viewTableContainer)]; 
_constraintContainerVertSpace = [containerVertConstraints lastObject]; 
[_viewContainer addConstraint:_constraintContainerVertSpace]; 

containerVertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=0)-[_viewOptions]-(>[email protected])-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_viewOptions)]; 
[_viewContainer addConstraints:containerVertConstraints]; 

但是,如果我試圖改變我的containerView和tableContainerView之間的垂直空間的限制,我得到的錯誤。 (我試圖調整tableContainerView與鍵盤的外觀收縮)

[UIView animateWithDuration:0.25f 
         delay:0 
        options:UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        _constraintContainerVertSpace.constant = -keyboardHeight; 
       } completion:nil]; 

這些是不能同時滿足約束條件:

(
"<NSIBPrototypingLayoutConstraint:0x140d7bb0 'IB auto generated at build time for view with fixed frame' V:|-(460)-[UIView:0x140ddef0] (Names: '|':UIView:0x140dde60)>", 
"<NSIBPrototypingLayoutConstraint:0x140d7c10 'IB auto generated at build time for view with fixed frame' V:[UIView:0x140ddef0(44)]>", 
"<NSLayoutConstraint:0x14d9db30 V:[UIView:0x140ddef0]-(0)-| (Names: '|':UIView:0x140dde60)>", 
"<NSIBPrototypingLayoutConstraint:0x140e0290 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x140dde60] (Names: '|':UIView:0x140dddd0)>", 
"<NSLayoutConstraint:0x14d9df60 V:[UIView:0x140dde60]-(-216)-| (Names: '|':UIView:0x140dddd0)>", 
"<NSIBPrototypingLayoutConstraint:0x140e0830 'IB auto generated at build time for view with fixed frame' V:|-(64)-[UIView:0x140dddd0] (Names: '|':UIView:0x140e0470)>", 
"<NSLayoutConstraint:0x14d9d820 V:[UIView:0x140dddd0]-(0)-| (Names: '|':UIView:0x140e0470)>", 
"<NSAutoresizingMaskLayoutConstraint:0x1403a0b0 h=--& v=--& V:[UIView:0x140e0470(568)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14d9db30 V:[UIView:0x140ddef0]-(0)-| (Names: '|':UIView:0x140dde60)> 

的UIView:0x140dde60在這種情況下是_viewTableContainer。 0x140ddef0 = _viewInputContainer。 0x140dddd0 = _viewContainer。

這是爲什麼NSIBPrototypingLayoutConstraint五:| - (460) - [UIView的:0x140ddef0(_viewInputContainer)]由IB正在創建當我_viewInputContainer之間設置translatesAutoresizingMaskIntoConstraints爲NO,並且當我已經定義明確的垂直的約束和它上面的_tableView?

我已經嘗試過在樂器中使用Cocoa Layout,但是有太多的噪音讓它有任何意義。

+0

看看這個非常類似的問題在這裏:http:// stackoverflow。com/questions/19015215/trouble-with-autolayout-on-uitableviewcell/19020006#19020006 – smileyborg

回答

18

除了使用translatesAutoresizingMaskIntoConstraints之外,我必須手動除去視圖上的約束。

[_viewContainer removeConstraints:_viewContainer.constraints]; 
[self.view removeConstraints:self.view.constraints]; 

添加這些更改後,動畫不會再出現錯誤。

10

爲了移除由IB生成的NSIBPrototypingLayoutConstraint,可以通過在IB上添加一些虛擬約束來解決它,並將它們設置爲remove at build time。然後,IB將不會爲您生成NSIBPrototypingLayoutConstraint

+2

@smileyborg給出[good link](http://stackoverflow.com/a/19020006/419348)。這個問題更清楚。 – AechoLiu

3

強制性斯威夫特:

view.removeConstraints(view.constraints) 

另外,如果你想刪除約束代碼一個特定的界面項,你可以刪除它們只是一個對象。以下是幾個示例:

myTableView.removeConstraints(myTableView.constraints) 
myButton.removeConstraints(myButton.constraints) 
myImageView.removeConstraints(myImageView.constraints) 

例如,如果您是創建付費或免費版本的應用程序,這可能特別有用。您可以在界面構建器中完成大部分界面佈局,然後使用Visual Format Language對代碼中的佈局進行調整。

+0

它適合我。謝謝! –