Change autodetection at runtime

Is it possible to change the autodetection restrictions at runtime?

I know that you can change the constant, but how would you change the different attributes.

For example, NSLayoutAttributeTop for NSLayoutAttributeBottom?

Here is a simple example of what I hope to achieve, it will set the shortcut on the top left, and then when you click the button, it will set the bottom mark on the right.

The original restrictions work as expected, pressing the button does not work properly and throws the infamous "Cannot satisfy the restrictions at the same time."

Here is the code I'm using:

    - (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.constraintA = [NSLayoutConstraint constraintWithItem:self.topLabel
                                                    attribute:NSLayoutAttributeTop
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:self.view
                                                    attribute:NSLayoutAttributeTop
                                                   multiplier:1.0
                                                     constant:0.0];

    self.constraintB = [NSLayoutConstraint constraintWithItem:self.topLabel
                                                    attribute:NSLayoutAttributeLeft
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:self.view
                                                    attribute:NSLayoutAttributeLeft
                                                   multiplier:1.0
                                                     constant:0.0];

    [self.view addConstraint:self.constraintA];
    [self.view addConstraint:self.constraintB];
}

- (IBAction)tappedChange:(id)sender
{

    [self.view removeConstraints:@[ self.constraintA, self.constraintB ]];

    self.constraintA = [NSLayoutConstraint constraintWithItem:self.topLabel
                                                    attribute:NSLayoutAttributeBottom
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:self.view
                                                    attribute:NSLayoutAttributeBottom
                                                   multiplier:1.0
                                                     constant:0.0];

    self.constraintB = [NSLayoutConstraint constraintWithItem:self.topLabel
                                                    attribute:NSLayoutAttributeRight
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:self.view
                                                    attribute:NSLayoutAttributeRight
                                                   multiplier:1.0
                                                     constant:0.0];
    [self.view addConstraint:self.constraintA];
    [self.view addConstraint:self.constraintB];
    [self.view setNeedsLayout];
}

Thank you for your time.

+4
source share
2 answers

Remove/Recreate/Add constraint iOS 7 . iOS (8 ), , .active NSLayoutConstraint, .

// Move to right
self.leadingConstraint.active = false;
self.trailingConstraint.active = true;

// Move to bottom
self.topConstraint.active = false;
self.bottomConstraint.active = true;

t9wXb.gif

Interface Builder, ( , ).

Xcode window showing an example

, , .

, API , :

po [[UIWindow keyWindow] _autolayoutTrace]
+5

Masonry - , , , , .

:

// Define some constraints, making the top one a @property:
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
}];

// Update the top constraint to match the bottom of the superview instead of the top
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    self.topConstraint = make.top.equalTo(superview.mas_bottom).with.offset(padding.bottom);
}];
0

Source: https://habr.com/ru/post/1615663/


All Articles