質問

I have the following simple test code in my controller:

- (void)loadView
{
    UIView *view = [UIView new];
    [self setView:view];

    UILabel *label = [UILabel new];
    [label setText:@"Hello World!"];

    [view addSubview:label];

    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[label]"
        options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
} 

The code fails with the following exception and I can't figure out why. Any help would be greatly appreciated:

2013-04-15 14:15:47.880 libmarkup-test[1072:c07] 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:0x753eb60 H:|-(NSSpace(20))-[UILabel:0x7536b60]   (Names: '|':UIView:0x75376a0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x712a2c0 h=--& v=--& UILabel:0x7536b60.midX ==>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x753eb60 H:|-(NSSpace(20))-[UILabel:0x7536b60]   (Names: '|':UIView:0x75376a0 )>

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.

The "unable to simultaneously satisfy constraints" message is especially confusing since, as far as I can tell, I'm only specifying one constraint.

役に立ちましたか?

解決

It looks like you forgot to set translatesAutoresizingMaskIntoConstraints on the UILabel. By default it will be YES. So the autoresizing mask on that label are translated to additional constraints, which are then conflicting with the one you specified.

Adding this should fix the constraint issue:

label.translatesAutoresizingMaskIntoConstraints = NO;

You should probably think about a vertical constraints on that label as well.

他のヒント

please adjust the constraint priority( default is 1000) and hugging priority(default is 250)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top