向现有 UIView 添加非活动约束

Adding non active constraints to existing UIView

我有 UIView 我在 IB 中设置了约束。现在在代码中,我想通过添加一些新约束并停用旧约束来制作一些动画。首先,我向视图添加新的约束,它是非活动的,如下所示:

// camera view new constraints
    self.cameraNewLeftConstraint = [NSLayoutConstraint constraintWithItem:self.cameraView
                                                                attribute:NSLayoutAttributeLeading
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:self.smallImageContainerView
                                                                attribute:NSLayoutAttributeLeading
                                                               multiplier:1.0
                                                                 constant:0];
    self.cameraNewLeftConstraint.active = NO;
    [self.presentationView addConstraint:self.cameraNewLeftConstraint];

但这会在运行时生成约束警告(stnadard 消息 - 将尝试破坏某些约束),尽管我添加了非活动约束,但我的约束存在冲突。我该如何处理,应该如何处理?

视图没有非活动约束。如果您向视图添加约束,则该约束将处于活动状态。

active 属性 设置为 YES 的新方法将约束添加到适合您的视图中。在您的代码中,当您调用 addConstraint 时,iOS 将您的约束添加到您的视图中,并将 active 更改为 YES

所以,不要在视图上调用 addConstraint。只需创建约束并在需要时将其 active 属性 设置为 YES