如何在两个自动布局约束之间切换?

And*_*ree 8 ios autolayout

我有两个UI布局约束,它们在设计上相互冲突.一次只能有一个活跃.

UIViewController's方法中updateConstraintsIfNeeded,我有以下代码在两个约束之间切换,具体取决于数据模型的状态.

override func updateConstraintsIfNeeded() {
    super.updateConstraintsIfNeeded()

    if question?.thumbURL != nil {
        showAttachmentConstraint.active = true
        hideAttachmentConstraint.active = false        
    } else {
        showAttachmentConstraint.active = false
        hideAttachmentConstraint.active = true
    }
}
Run Code Online (Sandbox Code Playgroud)

这项工作按计划进行,但我在调试输出中得到了这个熟悉的警告:

无法同时满足约束.可能至少下列列表中的一个约束是您不想要的约束....

显然,当语句showAttachmentConstraint.active = true执行时,它暂时与hideAttachmentConstraint当时仍处于活动状态的冲突相冲突.

是否可以将此切换操作原子化?我希望能有像beginUpdateendUpdateUITableView.

And*_*tta 6

您可以将其中一个冲突约束的优先级更改为999而不是1000.因此您在设计时甚至没有任何问题.