title: How to easily collapse vertical space around label when its text is nil?
body: <p>Suppose I have three labels that are laid out below each other in a column. The uppermost label's top edge is pinned to the superview's top edge. All following labels' top edges are pinned to the preceding label's bottom edge. The leading and trailing edges of all labels are pinned to the leading and trailing edge of the superview. Here's what it looks like in Interface Builder (I added a blue background on every label to visualize its extent).</p>
  
  <p><a href="https://i.stack.imgur.com/aMPoX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aMPoX.png" alt="Screenshot showing labels in interface builder"></a></p>
  
  <p>In the simulator the result looks like this.</p>
  
  <p><a href="https://i.stack.imgur.com/SAsar.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SAsar.png" alt="Simulator screenshot showing three labels"></a></p>
  
  <p>All labels are connected to outlets in a view controller.</p>
  
  <pre><code>@IBOutlet weak var label1: UILabel!
  @IBOutlet weak var label2: UILabel!
  @IBOutlet weak var label3: UILabel!
  </code></pre>
  
  <p>When I set the text of <code>label2</code> to <code>nil</code></p>
  
  <pre><code>label2.text = nil
  </code></pre>
  
  <p>the label itself collapses.</p>
  
  <p><a href="https://i.stack.imgur.com/GZO8o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GZO8o.png" alt="Simulator screenshot showing the collapsed middle label"></a></p>
  
  <p>However, the top and bottom spaces around the label do not collapse. This is evident by the fact that there is no blue background on the middle label in the last screenshot. As a result, the space between <code>label1</code> and <code>label3</code> is double the space of the layout in the first screenshot.</p>
  
  <p>My question is - <strong>on iOS8</strong> - what is the easiest way to collapse either the middle label's top or bottom space so that the two remaining labels still use the vertical spacing defined in the original layout? To be clear, this is the result I want to achieve.</p>
  
  <p><a href="https://i.stack.imgur.com/aYYVx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aYYVx.png" alt="Simulator screenshot showing reduced spacing between the two remaining labels"></a></p>
  
  <p>Options I've found so far:</p>
  
  <h2>Bottom/Top Spacing Constraint Outlet</h2>
  
  <p>Define an outlet for the middle label's top or bottom spacing constraint.</p>
  
  <pre><code>@IBOutlet weak var spacingConstraint: NSLayoutConstraint!
  </code></pre>
  
  <p>Store the constraint's initial constant into a variable (e.g. in <code>awakeFromNib</code> or <code>viewDidLoad</code>).</p>
  
  <pre><code>private var initialSpacing: CGFloat!
  
  override func viewDidLoad() {
      initialSpacing = spacingConstraint.constant
      ...
  </code></pre>
  
  <p>Set the constraint's constant to zero whenever the text is set to <code>nil</code> or back to its initial value when the text is not <code>nil</code>.</p>
  
  <pre><code>spacingConstraint.constant = label2.text == nil ? 0 : initialSpacing
  </code></pre>
  
  <p>This approach feels a bit clumsy since it requires two additional variables.</p>
  
  <h2>Height Constraint Outlet</h2>
  
  <p>Set the vertical spacing around the middle label to zero and increase its height by the same amount. Define an outlet for the height constraint and proceed as above, setting the height to zero when the text is <code>nil</code> and back to it's initial value when the height is not <code>nil</code>.</p>
  
  <p>This is still as clumsy as the previous approach. In addition, you have to hardcode the spacing and cannot use the built-in default spacings (blank fields in Interface builder).</p>
  
  <h2>UIStackView</h2>
  
  <p>This is not an option since <code>UIStackView</code> is only available on iOS 9 and above.</p>