我正在尝试为inputView创建自定义UITextView。我有一个正在工作的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.

任何帮助,将不胜感激。谢谢。

最佳答案

translatesAutoresizingMaskIntoConstraintslabel设置为false:

label.setTranslatesAutoresizingMaskIntoConstraints(false)

参见Adopting Auto Layout:

对于了解自动版式的视图,在大多数情况下,
想要将autoresizingMaskIntoConstraints转换为NO。原因是
通过翻译自动调整大小蒙版生成的约束
已经足以完全指定给定视图的框架
它的视角框架,通常太多了。例如这个
翻译将阻止按钮自动假定其按钮
标题更改时的最佳宽度。

根据经验,忘记在程序创建的视图上设置该标志必须是约束异常的第一大原因。

回答有关混合自动版式和自动调整大小的问题;两者可以自由混合和匹配。但是,说您的项目同时使用了这两种说法是不正确的。如果启用了“自动布局”,则该选项适用于整个项目。仍然可以使用“自动调整大小”来配置视图,但是实际上发生的是,这些规范已转换为“自动布局”约束。上面的问题是,您具有自动调整大小视图label-带有转换后的约束,与您指定的其他自动布局约束冲突。

关于ios - AutoResizingView内部的AutoLayout,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31535981/

10-10 03:58