我正在嘗試爲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.
任何幫助,將不勝感激。謝謝。
謝謝,這解決了我的問題。我之前曾嘗試過使用'translatesAutoresizingMaskIntoConstraints',但我將它設置在視圖上而不是標籤上。 – RPK
樂意幫忙!這很好,你有它排序。 –