ios 自动布局约束警告“将尝试通过打破约束来恢复”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25107762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autolayout constraints warning "Will attempt to recover by breaking constraint"
提问by valbu17
I am setting some specifics constraints for iPhone 4 when the app is ran on this device. Everything look great in terms of alignment and everything.
当应用程序在此设备上运行时,我正在为 iPhone 4 设置一些特定的约束。在对齐和一切方面,一切看起来都很棒。
But, I am getting an error for all my constraints that is trying to recover them by breaking the constraints.
但是,我的所有约束都出现错误,试图通过打破约束来恢复它们。
This method gets call in my viewDidLoad when it detects if its an iPhone 4
当它检测到它是否是 iPhone 4 时,此方法会在我的 viewDidLoad 中调用
- (void) addConstraints {
// removing automatic system constraints
[self.view removeConstraints:self.view.constraints];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(appBackground, myCustomer, myItemDetails, myItemQuantity, myItemPrice, myNext, myBtn); // UIImageView, UITextField, UIButton
for (UIView *view in [views allValues]) {
view.translatesAutoresizingMaskIntoConstraints = NO;
}
NSDictionary *metrics = @{@"width": @210.0, @"height": @42.0, @"verticalSpacing": @34};
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[appBackground]|"
options:0
metrics:nil
views:views];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[appBackground]|"
options:0
metrics:nil
views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(85)-[myCustomer(==height)]-(verticalSpacing)-[myItemDetails(==height)]-(verticalSpacing)-[myItemQuantity(==height)]-(verticalSpacing)-[myItemPrice(==height)]-(18)-[myNext(==35)]-(71.5)-[myBtn(==50)]-(7.5)-|"
options:0
metrics:metrics
views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[myCustomer(==width)]-|"
options:0
metrics:metrics
views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[myItemDetails(==width)]-|"
options:0
metrics:metrics
views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[myItemQuantity(==width)]-|"
options:0
metrics:metrics
views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[myItemPrice(==width)]-|"
options:0
metrics:metrics
views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-78-[myNext(==173)]-|"
options:0
metrics:metrics
views:views]];
constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-11.5-[myBtn(==54)]-|"
options:0
metrics:metrics
views:views]];
[self.view addConstraints:constraints];
}
Error!
错误!
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:0xb8282f0 H:|-(50)-[UITextField:0xb823fc0] (Names: '|':UIView:0xb82f360 )>",
"<NSLayoutConstraint:0xb828320 H:[UITextField:0xb823fc0(210)]>",
"<NSLayoutConstraint:0xb82be10 H:[UITextField:0xb823fc0]-(NSSpace(20))-| (Names: '|':UIView:0xb82f360 )>",
"<NSLayoutConstraint:0xb82d3d0 H:|-(78)-[UIButton:0xb82ad50] (Names: '|':UIView:0xb82f360 )>",
"<NSLayoutConstraint:0xb82d420 H:[UIButton:0xb82ad50(173)]>",
"<NSLayoutConstraint:0xb82d450 H:[UIButton:0xb82ad50]-(NSSpace(20))-| (Names: '|':UIView:0xb82f360 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xb82d450 H:[UIButton:0xb82ad50]-(NSSpace(20))-| (Names: '|':UIView:0xb82f360 )>
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.
2014-08-03 14:03:29.239 iReceipt[2886:607] 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:0xb8282f0 H:|-(50)-[UITextField:0xb823fc0] (Names: '|':UIView:0xb82f360 )>",
"<NSLayoutConstraint:0xb828320 H:[UITextField:0xb823fc0(210)]>",
"<NSLayoutConstraint:0xb82be10 H:[UITextField:0xb823fc0]-(NSSpace(20))-| (Names: '|':UIView:0xb82f360 )>",
"<NSLayoutConstraint:0xb82d480 H:|-(11.5)-[UIButton:0xb820120] (Names: '|':UIView:0xb82f360 )>",
"<NSLayoutConstraint:0xb82e390 H:[UIButton:0xb820120(54)]>",
"<NSLayoutConstraint:0xb82e3c0 H:[UIButton:0xb820120]-(NSSpace(20))-| (Names: '|':UIView:0xb82f360 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xb82e3c0 H:[UIButton:0xb820120]-(NSSpace(20))-| (Names: '|':UIView:0xb82f360 )>
Please let me know if you guys need more information..
如果你们需要更多信息,请告诉我..
回答by Ken Thomases
These three constraints:
这三个约束:
"<NSLayoutConstraint:0xb8282f0 H:|-(50)-[UITextField:0xb823fc0] (Names: '|':UIView:0xb82f360 )>",
"<NSLayoutConstraint:0xb828320 H:[UITextField:0xb823fc0(210)]>",
"<NSLayoutConstraint:0xb82be10 H:[UITextField:0xb823fc0]-(NSSpace(20))-| (Names: '|':UIView:0xb82f360 )>",
dictate that the superview be 50 + 210 + 20 == 280 points wide.
规定 superview 为 50 + 210 + 20 == 280 点宽。
These three constraints:
这三个约束:
"<NSLayoutConstraint:0xb82d3d0 H:|-(78)-[UIButton:0xb82ad50] (Names: '|':UIView:0xb82f360 )>",
"<NSLayoutConstraint:0xb82d420 H:[UIButton:0xb82ad50(173)]>",
"<NSLayoutConstraint:0xb82d450 H:[UIButton:0xb82ad50]-(NSSpace(20))-| (Names: '|':UIView:0xb82f360 )>"
dictate that the same superview be 78 + 173 + 20 == 271 points wide.
规定相同的超级视图为 78 + 173 + 20 == 271 点宽。
Obviously, those can't both be true at the same time. You need to decide what you really want to happen here. We can't read your mind and neither can UIKit.
显然,这些不可能同时成立。你需要决定你真正想要在这里发生什么。我们无法读懂您的想法,UIKit 也无法读懂。
Usually, you don't set a width constraint on a button; you let it use its intrinsic size with appropriate priorities for content hugging and compression resistance. Also, you may not wish to set hard spacing on either side of the button. Either let that spacing vary without constraint so the button can be its intrinsic size, lower the priority of that constraint so it's optional, or make an inequality so you have a minimum spacing but not an exact spacing.
通常,您不会在按钮上设置宽度约束;你让它使用它的内在大小和适当的优先级来进行内容拥抱和抗压缩。此外,您可能不希望在按钮的任一侧设置硬间距。要么让该间距不受约束地变化,以便按钮可以是其固有大小,降低该约束的优先级,使其成为可选,要么进行不等式,以便您有最小间距但不是精确间距。
回答by Hunkpapa
You are constraining your views too much. You must always leave for example one of leftmargin, width and rightmargin flexible, and let the autolayout engine stretch that one.
你限制了你的观点太多。例如,您必须始终让 leftmargin、width 和 rightmargin 之一保持灵活,并让自动布局引擎拉伸那个。
A single dash without any number means default distance, which is 20 pixels. So, if you remove your trailing constraints on
没有任何数字的单个破折号表示默认距离,即 20 像素。所以,如果你删除你的尾随约束
@"H:|-78-[myNext(==173)]-|"
@"H:|-11.5-[myBtn(==54)]-|"
and change them to
并将它们更改为
@"H:|-78-[myNext(==173)]"
@"H:|-11.5-[myBtn(==54)]"
the layout engine will then stretch the rightmargin of the button to make it fit the containing view. There are of course other ways of loosening up these constraints, all depending on your design goals.
然后布局引擎将拉伸按钮的右边距以使其适合包含视图。当然还有其他放松这些限制的方法,这一切都取决于您的设计目标。
Personally, I never use the standard API:s for autolayout anymore. I use the library PureLayout, which abstracts autolayout wonderfully!
就个人而言,我不再使用标准 API:s 进行自动布局。我使用库PureLayout,它很好地抽象了自动布局!