Reputation: 2941
I have a UIView with 4 buttons and their corresponding labels. They are currently centered in the view and the buttons in the middle have the appropriate trailing and leading space constraints necessary so that a defined space is between them at all times.
However, now when one of the buttons is hidden(and there will always just be one of the two middle buttons hidden at a time, maximum) the buttons need to be re-centered. I cant seem to delete the constraints related to trailing and leading spaces without Interface Builder barfing all over me and since I cannot just not have constraints for a subview, I want to just override all of the constraints of the buttons (except the width and height of the buttons themselves) that are related to the trailing(and or) leading spaces.
As an example without a visual picture
A----->B----->C----->D
Those are my four buttons with labels beneath(not shown of course) with four spaces between them(but all of them centered in the view). If B is hidden then i want re-center A, C and D in the view but now of course the trailing and leading spaces for the original set up are outdated and need to be reset according to this example. If C is hidden then I need to re-center and reset the constraints for A, B and D, where B would need to be centered in the view with A and D on either side with the same spacing (four spaces).
Is there a way to override these constraints in code like this? There doesnt seem to be a way to set up conditional constraints in XCode(although that would be nice) and there still isnt a way just to remove constraints in subviews so that you can create them all in code.
This solution needs to support iOS 7 & 8 and I cannot just turn off auto layout because other element in other views on the screen use constraints.
Any thoughts? Ideas?
Upvotes: 1
Views: 1021
Reputation: 890
The straight forward way would be to just redo your constraints.
That being said, do you have the need to re-show the hidden button? If not, an easy way I see is adding a low priority constraint that should kick-in when one button is hidden, and instead of just hiding the button you want to hide, remove it altogether from its superview.
Say, if button B could be hidden add a horizontal spacing constraint between A and C with priority lower than B's compression resistance priority, and constant that is the correct spacing between A and C when B is hidden. When you want to hide B, remove it from its superview (effectively removing contraints pertaining to it) and the low priority constraint should kick-in.
Upvotes: 0