Set constraints through code to an element from Storyboard with swift iOS8

前端 未结 3 1076
粉色の甜心
粉色の甜心 2020-12-21 16:22

I have a ViewController with a tableView. I\'ve set it up in the Storyboard. Is there a way to set the constraints for the tableView programmatically? I\'ve tried to set a I

3条回答
  •  囚心锁ツ
    2020-12-21 17:07

    You mentioned in one of your comments that you want to change the constraints when the user presses a button. You can achieve this in less code by making an outlet to the constraint. Find the constraint in the document outline page and CTRL + Drag to your class to create an outlet to the constraint. Then you can change the constant in code easier.

    @IBOutlet weak var topConstraint: NSLayoutConstraint!
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
    
    func buttonPressed() {
        if (/* some condition */) {
            self.topConstraint.constant += 8
            self.bottomConstraint.constant += 8
        } else {
            // Return the constraints to normal, or whatever you want to do
        }
    }
    

提交回复
热议问题