1

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
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
PandaDev
  • 121
  • 11
  • Does this answer your question? [Programmatically disable auto-layout constraint](https://stackoverflow.com/questions/26021044/programmatically-disable-auto-layout-constraint – rbaldwin May 17 '20 at 14:49
  • how do you know it's failing? any logs you can see in the console? try changing the value of the constant to see the difference of constraints. having a 5 is not really noticeable – Joshua May 17 '20 at 14:58

2 Answers2

1

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

Fabio
  • 5,432
  • 4
  • 22
  • 24
  • This worked Well, however I had some issues with non text fields / buttons I am currently looking into it thank you for the help... I will add your reply as the solution for the time being as I found it most helpful. – PandaDev Jun 07 '20 at 03:17
  • @PandaDev I'm glad that my answer was useful to you, now I'm out on business but if you need some help, write well ... As soon as I return I will try to answer you ... Have a nice day :) – Fabio Jun 08 '20 at 06:06
0

Probably you can check your @properties and replace weak with strong. Reason is, isActive = false will set self.yourConstraint = nil, hence you wouldn't be able to use self.yourConstraint again.

grow4gaurav
  • 3,145
  • 1
  • 11
  • 12
  • I tried that... it didnt seem to work... I am still looking into it however thanks for the input though, I may find it useful later in my development process. – PandaDev Jun 07 '20 at 03:15