AutoLayout with hidden UIViews?

后端 未结 13 1450
南方客
南方客 2020-11-27 09:27

I feel like it\'s a fairly common paradigm to show/hide UIViews, most often UILabels, depending on business logic. My question is, what is the best

13条回答
  •  误落风尘
    2020-11-27 09:57

    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.

提交回复
热议问题