简体   繁体   中英

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

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
            }
        }
}

Follow these steps

  1. Just select the constraint you want to update progrmatically eg. height
  2. create an outlet of it. New referencing outlet. 在此处输入图片说明
  3. eg. @IBOutlet weak var heightConstraint: NSLayoutConstraint!

  4. update constant, heightConstraint.constant = 100.

为需要修改的约束设置出口并将其连接起来。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM