AutoResizingView 内部的 AutoLayout

AutoLayout inside of an AutoResizingView

我正在尝试为 UITextView 创建自定义 inputView。我有一个我正在使用的 UIView 的子类,我正在尝试向其中添加 UI 元素。视图本身设置为 self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;,以便系统将视图填充到 inputView 应有的大小。

我似乎无法获得 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.

如有任何帮助,我们将不胜感激。谢谢。

labeltranslatesAutoresizingMaskIntoConstraints 设置为 false:

label.setTranslatesAutoresizingMaskIntoConstraints(false)

参见Adopting Auto Layout

For views that are aware of Auto Layout, in most circumstances you want translatesAutoresizingMaskIntoConstraints to be NO. The reason is that the constraints generated by translating the autoresizing mask are already sufficient to completely specify the frame of a view given its superview’s frame, which is generally too much. For example, this translation would prevent a button from automatically assuming its optimal width when its title is changed.

根据经验,忘记在以编程方式创建的视图上设置该标志必须是约束异常的首要原因。

回答您关于混合使用自动布局和自动调整大小的问题;两者都可以自由混合搭配。但是,说您的项目同时使用两者是不正确的。如果您启用了自动布局,那将适用于整个项目。视图可能仍然使用自动调整大小来配置,但实际发生的是这些规范正在被转换为自动布局约束。上面的问题是您有自动调整大小视图 - label - 带有翻译的约束,与您指定的其他自动布局约束冲突。