How can I change constraints priority in run time

后端 未结 8 1216
一整个雨季
一整个雨季 2020-12-12 16:07

I have a view which has dynamic height and I am trying to change this view height priority in run time.

Here is my part of code;

if (index == 0) {

          


        
相关标签:
8条回答
  • 2020-12-12 16:41

    I was facing the same issue. As some of the answer mentioned above in iOS 13 changing priority will work fine, However in iOS 12 it will lead to crash.

    I was able to fix this problem by creating IBOutlet NSLayoutConstraint to that specific constraint in Storyboard retaining its priority to 1000, below is the code for fix.

    if (Condition) {
        surveyViewHeightConstraint.constant = 0;
        surveyViewHeightConstraint.isActive = false;
    } else if (index == 1) {
        surveyViewHeightConstraint.constant = 163;
        surveyViewHeightConstraint.isActive = True;
    }
    

    Hope this helps!!! Cheers

    0 讨论(0)
  • 2020-12-12 16:42

    The way we have always handled this is by not changing the constraint constant, just the priority. For example, in your situation have two height constraints.

     heightConstraintOne (who's height is set in the storyboard at 150, and priority set at 750)
     heightConstraintTwo (who's height is set in the storyboard at 0, and priority set at 250)
    

    if you want to hide the view, you:

    heightConstraintTwo.priority = 999;
    

    likewise, if you want to show the view:

    heightConstraintTwo.priority = 250;
    
    0 讨论(0)
提交回复
热议问题