Riajur Rahman
Riajur Rahman

Reputation: 2034

programmatically update Autolayout Constraints which is set via Inspector

I have a ViewController which has a tableview in it. I set its autolayout manually. Now I'm trying to change the autolayout constraints programmatically. How can I do that?

This is my code's screenshot

Thanks

Upvotes: 0

Views: 209

Answers (3)

Ali Baqbani
Ali Baqbani

Reputation: 153

You can change constraints programatically without any outlet:

for (_, value) in self.view.constraints.enumerate() {
        let constraint = value as NSLayoutConstraint

        if constraint == .Height {
            if value.firstItem.isEqual(self.tableView) {
                constraint.constant = 200.0
            }
        }
}

Upvotes: 5

Chandan
Chandan

Reputation: 757

Follow these steps

  1. Just select the constraint you want to update progrmatically eg. height
  2. create an outlet of it. New referencing outlet. enter image description here
  3. eg. @IBOutlet weak var heightConstraint: NSLayoutConstraint!

  4. update constant, heightConstraint.constant = 100.

Upvotes: 2

Avi
Avi

Reputation: 7552

Set up outlets for the constraints you need to modify and hook them up.

Upvotes: 0

Related Questions