Dynamically set view top margin and height using autolayout - swift3


I am working on a UI where I have 3 labels. All are arranged vertically-

Label 1

Label 2

Label 3

  • At a time any two Label will be shown to user.
  • For example if Label 1 and Label 3 are shown then I want to shift Label3 up below the label1 and make Label2 height to 0.
  • If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0. So every time topmost label should have same top margin from superview. It can be either Label1, Label2 or Label3

Should I use stack view to achieve this requirement?

What is the best way to do it using auto layout?

  1. Use a Stack View
  2. You can hide/unhide views from the Stack View using the Stack View APIs to make the views disappear
  3. Stack Views will remove the view, and your two views will reposition at the top.