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

前端 未结 4 1347
有刺的猬
有刺的猬 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:58

    // method myView.topAnchor.constraintEqualToAnchor creates new one inactive anchor 
    // and not returns exist with equal relationships     
    
    // you can set identifier for any constraint in Interface Builder or code and find & update in code
    for ( NSLayoutConstraint *c in [self.view constraintsAffectingLayoutForAxis:UILayoutConstraintAxisHorizontal] )
    {
        if ( YES == [c.identifier isEqualToString:@"my.layout-constraint.id"] )
        {
            // Unlike the other properties, the constant can be modified
            // after constraint creation. 
            // Setting the constant on an existing constraint performs much better 
            // than removing the constraint and adding a new one that's exactly like 
            // the old except that it has a different constant.
    
            c.constant = 123;
    
            // if needed
            // [self.view setNeedsUpdateConstraints];
    
            break;
        }
    }
    

提交回复
热议问题