title: CollectionView Cell size to fit Label
body: <p>I have set up a collection view using which scrolls horizontally and displays the elements in the ‘companies’ array. </p>
  
  <p><a href="https://i.stack.imgur.com/NSAD8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NSAD8.png" alt="enter image description here"></a></p>
  
  <p>The cells automatically resize relative to the label width. </p>
  
  <p>I have used the code &amp; storyboard constraints below to achieve this:</p>
  
  <pre class="lang-html prettyprint-override"><code>class addTextStatus: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
  
      var companies: [String] = ["Everyone","Limited", "Friends", "Only Me", "Test Length Cells", ]
  
      @IBOutlet weak var collectionView: UICollectionView!
      @IBOutlet weak var userImage: UIImageView!
      override func viewDidLoad(){
  
                collectionView.register(UINib.init(nibName: "statusCompanyCollectionView", bundle: nil), forCellWithReuseIdentifier: "cell4")
  
          if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
              flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
  
          }
  
          userImage.layer.masksToBounds = false
          userImage.layer.cornerRadius = userImage.frame.height/2
          userImage.clipsToBounds = true
      }
      @IBOutlet weak var backButton: UIButton!
      @IBAction func backButton(_ sender: Any) {
  
         dismiss(animated: true, completion: nil)
  
      }
  
      override var preferredStatusBarStyle : UIStatusBarStyle {
          return UIStatusBarStyle.default  
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return self.companies.count
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
  
          let cell4 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell4", for: indexPath) as! statusCompanyCollectionView
  
          cell4.company.text = self.companies[indexPath.item]
  
          return cell4
      }
  
      }
  </code></pre>
  
  
  
  <p><a href="https://i.stack.imgur.com/tD2Xf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tD2Xf.png" alt="enter image description here"></a></p>
  
  <p>However the issue is that when I add a UIImage view behind the label and assign constraints which dictate that it should be the same size and position as the label do I get the unexpected result below:</p>
  
  <p><a href="https://i.stack.imgur.com/Gtfhj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gtfhj.png" alt="enter image description here"></a></p>
  
  <p>When in actuality I would like to achieve the following:</p>
  
  <p><a href="https://i.stack.imgur.com/mAz1H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mAz1H.png" alt="enter image description here"></a></p>
  
  <p>The constraints I have used to apply the image behind are below:</p>
  
  <p><a href="https://i.stack.imgur.com/YgTdq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YgTdq.png" alt="enter image description here"></a></p>
  
  <p>Interestingly when I use a UIView instead of a UIImage view it works as expected, I do however need to use image views in this instance.</p>
  
  <p>Why is this occurring and how do I resolve this issue? </p>