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

前端 未结 4 1364
有刺的猬
有刺的猬 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 02:49

    Here is an example declaring a constraint c which will be referenced later in time. We set a new constant value and then call layout on the superview.

    myView.translatesAutoresizingMaskIntoConstraints = false
    
    var constraints: [NSLayoutConstraint] = [
        myView.topAnchor.constraintEqualToAnchor(view.topAnchor),
        myView.leftAnchor.constraintEqualToAnchor(view.leftAnchor),
        myView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor)
    ]         
    let c = myView.rightAnchor.constraintEqualToAnchor(view.rightAnchor)       
    constraints.append(c)   
    
    view.addSubview(myView)
    NSLayoutConstraint.activateConstraints(constraints)
    
    // Some time later
    c.constant = -100
    view.setNeedsLayout()
    

提交回复
热议问题