How to change Auto Layout constraints after they are set when using constraintEqualToAnchor()?

前端 未结 4 1352
有刺的猬
有刺的猬 2021-02-04 02:15

I try to set up a view with AutoLayout constraints by using constraintEqualToAnchor():

override func viewDidLoad() {
    super.viewDidLoad()

    le         


        
4条回答
  •  日久生厌
    2021-02-04 03:05

    You need to deactivate the previous constraint when activating a new one so that you don't end up over constraining your view. To do that, store a reference to each of the constraints as a property in your ViewController and then set the active property of the old constraint to false before creating and activating the new constraint:

    Swift 2.x:

    class ViewController: UIViewController {
        var leftConstraint: NSLayoutConstraint?
        var trailingConstraint: NSLayoutConstraint?
        var topConstraint: NSLayoutConstraint?
        var bottomConstraint: NSLayoutConstraint?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let myView = UIView()
            myView.backgroundColor = UIColor.orangeColor()
            myView.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(myView)
    
            leftConstraint = myView.leftAnchor.constraintEqualToAnchor(view.leftAnchor)
            leftConstraint?.active = true
    
            trailingConstraint = myView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor)
            trailingConstraint?.active = true
    
            topConstraint = myView.topAnchor.constraintEqualToAnchor(view.topAnchor)
            topConstraint?.active = true
    
            bottomConstraint = myView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor)
            bottomConstraint?.active = true
    
            /******************************************/
            /* I try to change one of the constraints */
            /******************************************/
            leftConstraint?.active = false
            leftConstraint = myView.leftAnchor.constraintEqualToAnchor(view.rightAnchor, constant: -100)
            leftConstraint?.active = true
        }
    }
    

    Update for Swift 3 syntax:

    class ViewController: UIViewController {
        var leftConstraint: NSLayoutConstraint?
        var trailingConstraint: NSLayoutConstraint?
        var topConstraint: NSLayoutConstraint?
        var bottomConstraint: NSLayoutConstraint?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let myView = UIView()
            myView.backgroundColor = UIColor.orange
            myView.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(myView)
    
            leftConstraint = myView.leftAnchor.constraint(equalTo: view.leftAnchor)
            leftConstraint?.isActive = true
    
            trailingConstraint = myView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
            trailingConstraint?.isActive = true
    
            topConstraint = myView.topAnchor.constraint(equalTo: view.topAnchor)
            topConstraint?.isActive = true
    
            bottomConstraint = myView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
            bottomConstraint?.isActive = true
    
            /******************************************/
            /* I try to change one of the constraints */
            /******************************************/
            leftConstraint?.isActive = false
            leftConstraint = myView.leftAnchor.constraint(equalTo: view.rightAnchor, constant: -100)
            leftConstraint?.isActive = true
        }
    }
    

提交回复
热议问题