2015-07-21 40 views
1

我正在嘗試爲UITextView創建一個自定義inputView。我有一個我正在使用的UIView的子類,我正在嘗試向其添加UI元素。視圖本身設置爲self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;,以便系統將視圖填充inputView應該的大小。AutoResizingView中的AutoLayout

我似乎無法得到AutoLayout限制在這個視圖內工作。無論我嘗試多少不同的事情,我總是有相互衝突的約束。

是否可以在自動調整視圖內使用AutoLayout

下面是我在做什麼一些示例代碼:

UILabel *label = [[UILabel alloc] init]; 
label.text = @"Test Label"; 
label.textAlignment = NSTextAlignmentCenter; 

[self addSubview:label]; 

NSArray *labelHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8.0-[label]-8.0-|" options:0 metrics:nil views:@{ @"label" : label }]; 

NSArray *labelVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8.0-[label]" options:0 metrics:nil views:@{ @"label" : label }]; 

NSMutableArray *constraintsArray = [NSMutableArray array]; 
[constraintsArray addObjectsFromArray:labelHorizontalConstraints]; 
[constraintsArray addObjectsFromArray:labelVerticalConstraints]; 

[self addConstraints:constraintsArray]; 

這是我得到的那種錯誤:

2015-07-21 05:56:38.804 InputView[880:22401] 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:0x79175910 UIInputSetHostView:0x79175640.width == UIInputSetContainerView:0x79174ff0.width>", 
    "<NSLayoutConstraint:0x78f8f240 'UIView-Encapsulated-Layout-Width' H:[UIInputSetContainerView:0x79174ff0(768)]>", 
    "<NSLayoutConstraint:0x7932ffa0 H:|-(8)-[UILabel:0x79364df0'Test Label'] (Names: '|':TestInputView:0x79364f10)>", 
    "<NSLayoutConstraint:0x7932d1a0 H:[UILabel:0x79364df0'Test Label']-(8)-| (Names: '|':TestInputView:0x79364f10)>", 
    "<NSLayoutConstraint:0x793611b0 TestInputView:0x79364f10.right == UIInputSetHostView:0x79175640.right>", 
    "<NSLayoutConstraint:0x79361180 H:|-(0)-[TestInputView:0x79364f10](LTR) (Names: '|':UIInputSetHostView:0x79175640)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x79362920 h=--& v=--& UILabel:0x79364df0'Test Label'.midX ==>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7932d1a0 H:[UILabel:0x79364df0'Test Label']-(8)-| (Names: '|':TestInputView:0x79364f10)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2015-07-21 05:56:38.805 InputView[880:22401] 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:0x7932ffa0 H:|-(8)-[UILabel:0x79364df0'Test Label'] (Names: '|':TestInputView:0x79364f10)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x79362920 h=--& v=--& UILabel:0x79364df0'Test Label'.midX ==>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7932ffa0 H:|-(8)-[UILabel:0x79364df0'Test Label'] (Names: '|':TestInputView:0x79364f10)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2015-07-21 05:56:38.812 InputView[880:22401] 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:0x7932e7e0 V:|-(8)-[UILabel:0x79364df0'Test Label'] (Names: '|':TestInputView:0x79364f10)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x79362980 h=--& v=--& UILabel:0x79364df0'Test Label'.midY ==>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7932e7e0 V:|-(8)-[UILabel:0x79364df0'Test Label'] (Names: '|':TestInputView:0x79364f10)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

任何幫助,將不勝感激。謝謝。

回答

1

設置translatesAutoresizingMaskIntoConstraintslabel爲false:

label.setTranslatesAutoresizingMaskIntoConstraints(false) 

Adopting Auto Layout

對於意見認爲都知道汽車的佈局,在 想translatesAutoresizingMaskIntoConstraints是NO大多數情況下你。原因是 ,通過轉換自動存儲屏蔽 生成的約束已經足以完全指定其超視圖幀的視圖幀,這通常太多了。例如,這個 轉換會阻止按鈕在其標題更改時自動採用其最佳寬度。

根據經驗,忘記在程序創建的視圖上設置該標誌必須是約束異常的頭號原因。

要解答有關混合自動佈局和自動調整大小的問題;兩者可以自由混合和匹配。然而,說你的項目同時使用這兩種方法是不正確的。如果您啓用了自動佈局,則適用於整個項目。視圖可能仍然使用自動調整大小進行配置,但實際發生的情況是這些規範正在轉換爲自動佈局約束。上面的問題是,您有自動調整大小視圖 - label - 它的轉換約束與您指定的其他自動佈局約束衝突。

+0

謝謝,這解決了我的問題。我之前曾嘗試過使用'translatesAutoresizingMaskIntoConstraints',但我將它設置在視圖上而不是標籤上。 – RPK

+0

樂意幫忙!這很好,你有它排序。 –