2013-03-11 80 views
11

我正嘗試以編程方式在我的iOS應用程序中使用自動佈局。 我有這個初始化代碼以編程方式使用自動佈局約束條件

UIView *innerView = [[UIView alloc] initWithFrame:self.view.bounds]; 

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button1 setFrame:CGRectMake(50, 50, 150, 50)]; 
[button1 setTitle:@"button1" forState:UIControlStateNormal]; 

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button2 setFrame:CGRectMake(250, 50, 150, 50)]; 
[button2 setTitle:@"button2" forState:UIControlStateNormal]; 

[innerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[button1][button2(==button1)]|" options:NSLayoutFormatAlignAllBaseline metrics:0 views:NSDictionaryOfVariableBindings(button1, button2)]]; 

[innerView addSubview:button1]; 
[innerView addSubview:button2]; 
[self.view addSubview:innerView]; 

一個簡單的視圖控制器但是,當我試圖運行它,我得到這個錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview 
|[button1][button2(==button1)]| 
      ^' 

這有什麼錯呢?

,當我試圖消除在constraintsWithVisualFormat管,我得到這樣的警告:

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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x716be10 h=--& v=--& UIRoundedRectButton:0x71631f0.midX == + 325>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7169940 h=--& v=--& H:[UIRoundedRectButton:0x715fb80(150)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7169860 h=--& v=--& UIRoundedRectButton:0x715fb80.midX == + 125>", 
    "<NSLayoutConstraint:0x7164cd0 UIRoundedRectButton:0x71631f0.width == UIRoundedRectButton:0x715fb80.width>", 
    "<NSLayoutConstraint:0x7164940 H:[UIRoundedRectButton:0x715fb80]-(0)-[UIRoundedRectButton:0x71631f0]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7164940 H:[UIRoundedRectButton:0x715fb80]-(0)-[UIRoundedRectButton:0x71631f0]> 

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. 

而且由於這個結果,我的約束都完全沒有影響。 我在做什麼錯?

回答

34

你有三個問題:

首先,你需要選擇加入到基於佈局約束您既您的按鈕子視圖

[button1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[button2 setTranslatesAutoresizingMaskIntoConstraints:NO]; 

這將擺脫NSAutoresizingMaskLayoutConstraint錯誤。現在您可以瞭解您的佈局約束條件。

其次,您必須在添加約束之前addSubview。在button1和button2是innerview的子視圖之前,您在innerView上調用addContraints

第三,您應該指定(儘管文檔指出它是可選的)佈局字符串是垂直還是水平。​​。您還需要像@"V:[button1]|"這樣的垂直約束,它可以將button1對齊到innerview的底部,並且因爲您已將按鈕上的基線對齊,所以會出現button2。

+1

沒錯,'setTranslatesAutoresizingMaskIntoConstraints:NO'幫我擺脫這些錯誤的。但結果我得到button2與左上角對齊(button1不可見),但我仍然不能在'visualFormat'中使用管道。你有什麼想法爲什麼錯誤說'相關的視圖沒有超視圖。 UIViewController的'self.view'是我的'innerView'的超級視圖,不是嗎? 「 – bulbform 2013-03-11 19:36:01

+2

」相關視圖沒有超級視圖「是因爲您添加了一個涉及尚未被插入到視圖層次結構中的視圖的約束。在所有調用addSubview:之後移動addConstraints:call。 – escrafford 2013-03-11 19:50:43

+0

謝謝,當我在'addSubview'語句中移動帶有約束的代碼塊時,關於'無法解釋管道'的錯誤消失了,並且我的佈局與我想要的類似)謝謝,問題已解決) – bulbform 2013-03-11 19:54:38

2

只是爲了記錄:關於失蹤的上海華異常被已被觸發constraintsWithVisualFormat

相關問題