title: UIView child automatic frame size
body: <p>I am trying to add custom UIView (LabelIcon) to a parent view, that should not care about size of the child - he should just stack them with full width under each other based on the height of the child (which will vary).</p>
  
  <p>Currently this is how I add a child in the parent:</p>
  
  <pre><code>    let locationLabel = LabelIcon(frame: .zero, icon: "location", text: "Lane\nlane")
      let locationLabel2 = LabelIcon(frame: .zero, icon: "location", text: "Second text line\nLine line line\nAnother line\nsome more line")
      content.addSubview(locationLabel)
      content.addSubview(locationLabel2)
  </code></pre>
  
  <p>(I don't want to set manually the frame here - should be automatic)</p>
  
  <p>Constraints of these views so they are at least somehow visible (since I didn't set the frame):</p>
  
  <pre><code>    locationLabel.snp.makeConstraints { (make) in
          make.top.left.right.equalToSuperview()
          make.height.equalTo(40)
      }
  
      locationLabel2.snp.makeConstraints { (make) in
          make.left.right.equalToSuperview()
          make.top.equalTo(locationLabel.snp.bottom)
          make.height.equalTo(40)
      }
  </code></pre>
  
  <p>This could work, but it would be painful to do this for each element. Also I don't know the height of inner element right now.</p>
  
  <p>What I want to achieve is to not have to define any hardcoded constraints in parent. The child view should decide for himself how tall it will be.</p>
  
  <p>The child is defined this way:</p>
  
  <pre><code>class LabelIcon: UIView {
  
      init(frame: CGRect, icon: String, text: String) {
          super.init(frame: frame)
  
          self.backgroundColor = UIColor.orange
  
          let iconImageView = UIImageView(image: UIImage(named: icon))
          iconImageView.contentMode = UIViewContentMode.scaleAspectFit
          self.addSubview(iconImageView)
  
  
          let label = UILabel()
          label.textAlignment = .left
          label.numberOfLines = 0
          label.text = text
          self.addSubview(label)
  
          iconImageView.snp.makeConstraints { (make) in
              make.top.equalToSuperview().offset(5)
              make.left.equalToSuperview().offset(10)
          }
  
          label.snp.makeConstraints { (make) in
              make.top.equalToSuperview()
              make.left.equalTo(iconImageView.snp.right).offset(20)
  
          }
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  }
  </code></pre>
  
  <p>How can I define child in a way that it is aware of parent's width, but height will be done automatically based on the label size? The text will have dynamic number of lines.</p>
  
  <p>In parent there will be many views like this, therefore I want to have there as few constraints as possible (not to manually do it for every LabelIcon instance).</p>
  
  <p>Based on this code this is the current state:
  <a href="https://i.stack.imgur.com/mOycq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mOycq.png" alt="enter image description here"></a></p>
  
  <p>All my views are done programatically with autolayout (with SnapKit, no storyboard or xib). I would like to use UIStackView, but anyway the child has to have defined height in order for it to work.</p>
  
  <p><strong>EDIT:</strong>
  Views itself are visible when using these constraints:</p>
  
  <pre><code>locationLabel.snp.makeConstraints { (make) in
      make.top.left.right.equalToSuperview()
  
  }
  locationLabel2.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.top.equalTo(locationLabel.snp.bottom)
  }
  </code></pre>
  
  <p>Bu the views are over each other (label2 is not below label). Also the view is basically 0,0 in dimensions - there is no orange background:
  <a href="https://i.stack.imgur.com/rPZVT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rPZVT.png" alt="enter image description here"></a></p>
  
  <p><strong>From accepted answer:</strong></p>
  
  <p>Label constraints:</p>
  
  <pre><code>label.snp.makeConstraints { (make) in
      make.top.equalToSuperview()
      make.left.equalTo(iconImageView.snp.right).offset(20)
      make.bottom.equalToSuperview().offset(-10)
      make.right.equalToSuperview().offset(-10)
  }
  </code></pre>
  
  <p>Parent:</p>
  
  <pre><code>locationLabel.snp.makeConstraints { (make) in
      make.top.left.equalToSuperview()
  
  }
  locationLabel2.snp.makeConstraints { (make) in
      make.left.equalToSuperview()
      make.top.equalTo(locationLabel.snp.bottom)
  }
  </code></pre>
  
  <p>Result:</p>
  
  <p><a href="https://i.stack.imgur.com/nvQnc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nvQnc.png" alt="enter image description here"></a></p>