Search code examples
swiftswift5xcode11.4

Updating constraints in swift 5


I have tried deactivating and reactivating... However I cannot seem to find out how to do it... I have code to use as an example but it keeps failing me, if anyone knows how to update a constraint in swift 5 please let me know... I know that the updateConstraint() exists just not sure how to use it.

let constraint1: NSLayoutConstraint? = text.bottomAnchor.constraint(equalTo: view.bottomAnchor)

let constraint2: NSLayoutConstraint? = text.bottomAnchor.constraint (equalTo: view.bottomAnchor, constant: 5)

if acondition {
constraint1?.isActive = false
constraint2.isActive = true
} else {
constraint2.isActive = false
constraint1.isActive = true
}

Solution

  • first declare the variable, after set start constraints variable to true and after call a function that active and dis active constraints, try with my example:

    under controller class declare a button and a view do you want to move:

    let dummyView = UIView()
    let button = UIButton(type: .system)
    

    after declare variable to move your view with constraint:

    var up: NSLayoutConstraint?
    var down: NSLayoutConstraint?
    

    in ViewDiLoad set the view, the button and the constraints like this:

    dummyView.backgroundColor = .yellow
        dummyView.translatesAutoresizingMaskIntoConstraints = false
    
        button.setTitle("viewDiwn", for: .normal)
        button.backgroundColor = .red
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 16)
        button.addTarget(self, action: #selector(handletapButton), for: .touchUpInside)// button action that call handletapButton func
        button.translatesAutoresizingMaskIntoConstraints = false
    
        view.addSubview(dummyView)
        up = dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
        up?.isActive = true // start constraint active
        down = dummyView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100) // constraint to activate for move the view
    
        dummyView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        dummyView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        dummyView.heightAnchor.constraint(equalToConstant: 50).isActive = true
    
        view.addSubview(button)
        button.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        button.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        button.heightAnchor.constraint(equalToConstant: 50).isActive = true
        button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    

    now write the function to move the view by constraint animation:

    @objc func handletapButton() {
        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
            self.up?.isActive = false
            self.down?.isActive = true
            self.view.layoutIfNeeded()
        }, completion: nil)
    }
    

    this is the result

    enter image description here