title: Xcode swift view wrap content
body: <p>I'm an Android developer trying my hand at Xcode and it's been unpleasant so far. What I'm trying to do is have a custom view that has three sub views:</p>
  
  <ul>
  <li>UIImageView (for an icon)</li>
  <li>UILabel (for the title)</li>
  <li>UILabel (for the content)</li>
  </ul>
  
  <p>I want it such that the content label's height grows and shrinks to wrap the text it contains (like Android's <code>wrap_content</code>). And then, I want the custom view to also grow and shrink to wrap all three sub views.</p>
  
  <p>However, I cannot, for the life of me, figure out how these auto layouts/constraints work.</p>
  
  <p>01) How would I make my UILabel's height grow/shrink to match its contained text?</p>
  
  <p>02) How would I make my custom view's height grow/shrink to match its contained sub views?</p>
  
  <pre><code>override func layoutSubviews() {
      super.layoutSubviews()
      img_icon = UIImageView()
      txt_title = UILabel()
      txt_content = UILabel()
  
      img_icon.backgroundColor = Palette.white
      img_icon.image = icon
      txt_title.text = title
      txt_title.textAlignment = .Center
      txt_title.font = UIFont(name: "Roboto-Bold", size:14)
      txt_title.textColor = Palette.txt_heading1
      txt_content.text = content
      txt_content.textAlignment = .Center
      txt_content.font = UIFont(name: "Roboto-Regular", size:12)
      txt_content.textColor = Palette.txt_dark
      txt_content.numberOfLines = 0
      txt_content.preferredMaxLayoutWidth = self.frame.width
      txt_content.lineBreakMode = NSLineBreakMode.ByWordWrapping
      self.backgroundColor = Palette.white
      addSubview(img_icon)
      addSubview(txt_title)
      addSubview(txt_content)
  
      /*snip img_icon and txt_title constraints*/
  
      let txt_content_x = NSLayoutConstraint(item: txt_content, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0)
      let txt_content_y = NSLayoutConstraint(item: txt_content, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 80)
      let txt_content_w = NSLayoutConstraint(item: txt_content, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: 0)
      let txt_content_h = NSLayoutConstraint(item: txt_content, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
      txt_content.translatesAutoresizingMaskIntoConstraints = false
      NSLayoutConstraint.activateConstraints([
          txt_content_x,
          txt_content_y,
          txt_content_w,
          txt_content_h
      ])
  }
  </code></pre>
  
  <p>I understand that, in the above code I've tried, I have the height set to a constant <code>40</code>. This is only because I don't know how to achieve what I want.</p>
  
  <p>[EDIT]</p>
  
  <p>I've tried setting the height constraint to greater than or equal to but it just crashes Xcode.</p>
  
  <p>[EDIT]</p>
  
  <p>It crashes Xcode if I try to view it but works perfectly fine in the simulator. Question now is, <em>why</em>?</p>
  
  <p>My height constraint is now:</p>
  
  <pre><code>let txt_content_h = NSLayoutConstraint(item: txt_content, attribute: .Height, relatedBy: .GreaterThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
  </code></pre>
  
  <p>It works in the simulator and has the desired behaviour. However, if I open the storyboard that contains the view, it crashes. It's definitely that line of code because changing it back to <code>.Equal</code> resolves the crash.</p>
  
  <p>[EDIT]</p>
  
  <p>My temporary fix is:</p>
  
  <pre><code>#if TARGET_INTERFACE_BUILDER
      //use .Equal for height constraint
  #else
      //use .GreaterThanOrEqual for height constraint
  #endif
  </code></pre>
  
  <p>This way, it doesn't crash Xcode and still renders the way I want it on the simulator.</p>
  
  <p>[EDIT]</p>
  
  <p>I removed the pre-processor check because I realized there's no actual thing like that defined and it <em>still</em> works now. I swear I've changed <em>nothing else</em>.</p>
  
  <p>I am <em>this</em> close to giving up on iOS development because the interface builder keeps crashing Xcode without a reason when everything works in the simulator. Then, I do some nonsense edits and it works fine again.</p>