AutoLayout with hidden UIViews?

My personal preference for showing/hiding views is to create an IBOutlet with the appropriate width or height constraint.

I then update the constant value to 0 to hide, or whatever the value should be to show.

The big advantage of this technique is that relative constraints will be maintained. For example let’s say you have view A and view B with a horizontal gap of x. When view A width constant is set to 0.f then view B will move left to fill that space.

There’s no need to add or remove constraints, which is a heavyweight operation. Simply updating the constraint’s constant will do the trick.

Leave a Comment