How to switch between the two limitations of auto-layout?

I have two interface layout restrictions that conflict with each other in design. Only one of them can be active at a time.

In the UIViewController'smethod updateConstraintsIfNeeded, I have the following code that switches between two constraints, depending on the state of the data model.

    override func updateConstraintsIfNeeded() {
        super.updateConstraintsIfNeeded()

        if question?.thumbURL != nil {
            showAttachmentConstraint.active = true
            hideAttachmentConstraint.active = false

        } else {
            showAttachmentConstraint.active = false
            hideAttachmentConstraint.active = true
        }
    }

This work is as intended, but I received this familiar warning on debug output.

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. ...

Apparently, when the statement is executed showAttachmentConstraint.active = true, it temporarily conflicts with hideAttachmentConstraintwhich is still active at this time.

Is it possible to do this atom switching operation? I hope there is something like beginUpdateand endUpdatein UITableView.

+4
2

999 1000. , .

+5

, :

override func updateConstraintsIfNeeded() {
    super.updateConstraintsIfNeeded()

    if question?.thumbURL != nil {
        hideAttachmentConstraint.active = false
        showAttachmentConstraint.active = true
    } else {
        showAttachmentConstraint.active = false
        hideAttachmentConstraint.active = true
    }
}
+1

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


All Articles