0

例如,假设我有视图 a、b 和 c,并且我这样做:

[a setTranslatesAutoresizingMaskIntoConstraints:NO];
[b setTranslatesAutoresizingMaskIntoConstraints:NO];
[c setTranslatesAutoresizingMaskIntoConstraints:NO];

[a addSubview:b];
[a addSubview:c];

[a addConstraint:
 [NSLayoutConstraint constraintWithItem:b
                              attribute:NSLayoutAttributeLeft
                              relatedBy:NSLayoutRelationEqual
                                 toItem:a
                              attribute:NSLayoutAttributeLeft
                             multiplier:1.0
                               constant:0]];
[a addConstraint:
 [NSLayoutConstraint constraintWithItem:b
                              attribute:NSLayoutAttributeRight
                              relatedBy:NSLayoutRelationEqual
                                 toItem:a
                              attribute:NSLayoutAttributeRight
                             multiplier:1.0
                               constant:0]];

那么,我应该能够做到:

[a addConstraint:
 [NSLayoutConstraint constraintWithItem:c
                              attribute:NSLayoutAttributeWidth
                              relatedBy:NSLayoutRelationEqual
                                 toItem:a
                              attribute:NSLayoutAttributeWidth
                             multiplier:1.0
                               constant:0]];

?

如果我改为将最后一个命令更改为:

[a addConstraint:
 [NSLayoutConstraint constraintWithItem:c
                              attribute:NSLayoutAttributeWidth
                              relatedBy:NSLayoutRelationEqual
                                 toItem:a
                              attribute:NSLayoutAttributeRight
                             multiplier:1.0
                               constant:0]];

...然后一切正常。是什么赋予了?

4

0 回答 0