2
votes

I have a UIScrollView, and inside that, a UIView that defines the ContentSize of the ScrollView. Inside that ContentView, I have a couple of different Views, that are all of equal width (the maximum width), and all have proportional height to the Superview (even above the UIScrollView). The setup works perfectly and the scrolling works.

Now, I want to be able to hide one of those UIViews if there is no content to be shown inside, and in doing so, I want the other views to resize themselves to they fill up the blank space left behind.

I have a height constraint of that particular UIView to the Superview with a multiplier of 0.3 I added a second height constraint, of the exact same UIView to the exact same Superview with a multiplier of 0.01, but a lower priority, so that it doesn't affect it at runtime. (I've tested the situation: if I put the 0.3 multiplier on lower priority and the 0.01 multiplier on higher priority, everything is resized perfectly, exactly like I want it).

Now I created two outlets with connections in my header files, and here is the code I use when I want to make the change:

//DescriptionViewNormalConstraint is the 0.3 height one, with 1000 priority
        descriptionViewNormalConstraint.active = NO;
//DescriptionViewInvisibleConstraint is the 0.01 height one, with 500 priority
        descriptionViewInvisibleConstraint.active = YES;
        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];

But this doesn't work. In fact, it changes nothing. What am I missing here?

2
what do you mean with doens't work? do you mean that the descriptionViewNormalConstraint remains active? - ddb
Yes, the other constraint doesn't work. Or maybe it's activated but the autolayout isn't "refreshed". - el-flor
did you try to call setNeedsLayout and layoutIfNeeded over the specific view to which you attached these constraints? - ddb
Yes I've tried that, to set it on the childView that I try to get to a height of 0.01... But it doesn't change anything. - el-flor
do you need to re-active the descriptionViewNormalConstraint after you de-activated it? - ddb

2 Answers

5
votes

The solution was that I had to change the priority of both constraints. Set the constraint you want to use by default to a priority of anything lower then 1000. For instance, 750. Set the priority of the other constraint to 250. (In Storyboard).

Once you want the second constraint to replace the first one, simply change the priority of the second constraint from 250 to anything higher than 750, and it will magically work.

0
votes

If you do not need to re-active the descriptionViewNormalConstraint after you deactivate it, then you can simply remove that constraint from the view it is applied to with the following code

[myCustomView removeConstraint:descriptionViewNormalConstraint]; 

and then refresh. Let me know if it works