title: Unable to programatically align UIImageView to the centre of UICollectionViewCell
body: <p>Currently, I have a <code>UICollectionViewCell</code> used in horizontal scroll <code>UICollectionView</code>. I have tried the following to centre the icon to the centre.</p>
  <pre><code>class BasicCell: UICollectionViewCell {
      
      var tabInfo: TabInfo?
      
      let imageView: UIImageView = {
          let image = UIImage(systemName: &quot;gear&quot;)
          let imageView = UIImageView(image: image)
          return imageView
      }()
      
      override init(frame: CGRect) {
          super.init(frame: frame)
          
          addSubview(imageView)
  
          // Hoping we can center imageView. But it would not work? Why?
          addConstraint(NSLayoutConstraint.init(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
          addConstraint(NSLayoutConstraint.init(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
  
          BasicCell.topRoundCorners(self.contentView)
      }
  </code></pre>
  <p>Here's the outcome</p>
  <p><a href="https://i.stack.imgur.com/fc7e0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fc7e0.png" alt="enter image description here" /></a></p>
  <p>I expect the following code can centre the <code>UIImageView</code> without issue. However, it doesn't help</p>
  <pre><code>addConstraint(NSLayoutConstraint.init(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
  addConstraint(NSLayoutConstraint.init(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
  </code></pre>
  <p>At first, I was thinking to give up the programatically way, and create UI layout via storyboard. However, in my storyboard, there is no <code>UICollectionViewController</code>. (The top tab bar component, is a <code>UIView</code> holding a <code>UICollectionView</code> created programatically).</p>
  <p>Without <code>UICollectionViewController</code>, I don't find a way to drag and drop a <code>UICollectionViewCell</code> into the storyboard.</p>
  <p>Do you have any idea, how I can centre my <code>UIImageView</code>, with correct programatically code? Or, should I try to fix this problem with XIB? Thanks.</p>