Snapkit: How to replace a constraint?

Created on 10 Jan 2018  路  4Comments  路  Source: SnapKit/SnapKit

I setup constraint for messageInputContainerView before:
swift = messageInputContainerView.snp.makeConstraints { (maker) in maker.height.equalTo(48) maker.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(0) maker.leading.equalToSuperview().offset(0) maker.trailing.equalToSuperview().offset(0) }
And after, I want to replace maker.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(0)
by maker.bottom.equalToSuperview().offset(padding)
How to do it with SnapKit? Replace or delete and add a new constraint
Thank you

Most helpful comment

@phongle6893 I think that the most clear approach is to create both constraints in makeConstraints block, save them as a variables and activate and deactivate them as needed.

var safeAreaBottomConstraint: Constraint?
var superviewBottomConstraint: Constraint?

func setupLayout() {
  view.snp.makeConstraints { make in 
     safeAreaBottomConstraint = make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).constraint
     superviewBottomConstraint = make.bottom.equalToSuperview().constraint
  }
  safeAreaBottomConstraint?.activate()
  superviewBottomConstraint?.deactivate()
}

func updateState() {
  let something: Bool = ...
  if something  {
    safeAreaBottomConstraint?.activate()
    superviewBottomConstraint?.deactivate()    
  } else {
    safeAreaBottomConstraint?.deactivate()
    superviewBottomConstraint?.activate()
  }

  view.layoutIfNeeded()
}

All 4 comments

snp.removeConstraints() will remove all constraints. Unless you want to remove all and add all them back again. You can use snp.remake but you have to include all 4 constraint you need not just bottom.
What you want is the mechanism to update a single constraint. Check the

Hold on for dear life section

in SnapKit documentation for example code.

@phongle6893 I think that the most clear approach is to create both constraints in makeConstraints block, save them as a variables and activate and deactivate them as needed.

var safeAreaBottomConstraint: Constraint?
var superviewBottomConstraint: Constraint?

func setupLayout() {
  view.snp.makeConstraints { make in 
     safeAreaBottomConstraint = make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).constraint
     superviewBottomConstraint = make.bottom.equalToSuperview().constraint
  }
  safeAreaBottomConstraint?.activate()
  superviewBottomConstraint?.deactivate()
}

func updateState() {
  let something: Bool = ...
  if something  {
    safeAreaBottomConstraint?.activate()
    superviewBottomConstraint?.deactivate()    
  } else {
    safeAreaBottomConstraint?.deactivate()
    superviewBottomConstraint?.activate()
  }

  view.layoutIfNeeded()
}

@danshevluk that's can work,but if write superviewBottomConstraint before the safeAreaBottomConstraint, the console will print warning or error

@phongle6893 Your code should look like this.

var bottomSafeArea: Constraint?
var bottomSuperView: Constraint?

func setupLayout() {
messageInputContainerView.snp.makeConstraints { (maker) in
    maker.height.equalTo(48)
    bottomSafeArea = maker.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(0).constraint       
    maker.leading.equalToSuperview().offset(0)
    maker.trailing.equalToSuperview().offset(0)
}

bottomSafeArea?.deactivate()

messageInputContainerView.snp.makeConstraints { (maker) in
    bottomSuperView = maker.bottom.equalToSuperview().offset(padding)
}

}

After that bottomSuperView is active and no console error.

You can now simply.

bottomSuperView.deactivate()
bottomSafeArea.activate()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

cooler333 picture cooler333  路  5Comments

lolgear picture lolgear  路  5Comments

Cookiezby picture Cookiezby  路  8Comments

cjwirth picture cjwirth  路  4Comments

Shehryar picture Shehryar  路  7Comments