自动布局约束警告“将尝试通过打破约束来恢复". [英] Autolayout constraints warning "Will attempt to recover by breaking constraint"

查看:137
本文介绍了自动布局约束警告“将尝试通过打破约束来恢复".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此设备上运行该应用程序时,我正在为iPhone 4设置一些特定的限制.就对齐方式和一切而言,一切看起来都很不错.

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.

但是,我遇到的所有约束都出错,试图通过打破约束来恢复约​​束.

But, I am getting an error for all my constraints that is trying to recover them by breaking the constraints.

当该方法检测到其iPhone 4是否在我的viewDidLoad中被调用

This method gets call in my viewDidLoad when it detects if its an iPhone 4

- (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];

}

错误!

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..

推荐答案

这三个约束:

"<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 )>",

指示超级视图的宽度为50 + 210 + 20 == 280点.

dictate that the superview be 50 + 210 + 20 == 280 points wide.

这三个约束:

"<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 )>"

指示同一超级视图的宽度为78 + 173 + 20 == 271点.

dictate that the same superview be 78 + 173 + 20 == 271 points wide.

很显然,这些不能同时成立.您需要确定您真正想要在这里发生的事情.我们看不懂您的想法,UIKit也看不到.

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.

通常,您无需在按钮上设置宽度限制;您可以将其固有大小与适当的优先级结合使用,以实现内容拥抱和抗压缩性.另外,您可能不希望在按钮的两侧设置硬间距.要么让该间距无限制地变化,要么使按钮成为其固有大小,降低按钮的优先级,使其成为可选,或者使不等式使您具有最小的间距,但没有确切的间距.

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.

这篇关于自动布局约束警告“将尝试通过打破约束来恢复".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆