title: Swift iOS- How to Add Button to the outside a CollectionView Cell
body: <ol>
  <li>I want to get a deleteButton that is a delete image to sit on the outside of a collectionViewCell (it can be partially inside). </li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/wz70u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wz70u.png" alt="enter image description here"></a></p>
  
  <ol start="2">
  <li><p>I looked at other SO answers and they said just play with the button's frame's Rect's <code>x &amp; y</code> values which I did. When I set the <code>x &amp; y</code> values to <code>0,0</code> I get:</p>
  
  <pre><code> class CustomCell: UICollectionViewCell {
  
   @IBOutlet weak var imageView: UIImageView!
   var deleteButton: UIButton!
   var deleteButtonImg: UIImage!
  
   override func awakeFromNib() {
       super.awakeFromNib()
  
   deleteButton = UIButton(frame: CGRect(x: 0, y: 0, width: frame.size.width/4, height: frame.size.width/4))
  
   deleteButtonImg = UIImage(named: "delete-icon")!.withRenderingMode(.alwaysTemplate)
   deleteButton.setImage(deleteButtonImg, for: .normal)
   deleteButton.tintColor = UIColor.red
  
   contentView.addSubview(deleteButton)
   }
  </code></pre></li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/HseFg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HseFg.png" alt="enter image description here"></a></p>
  
  <ol start="3">
  <li><p>When I try to set the the Rect's <code>x &amp; y</code> to <code>-10,-10</code> the deleteButton gets clipped inside the cell's imageView. I don't have <code>clipsToBounds</code> set.</p>
  
  <pre><code> deleteButton = UIButton(frame: CGRect(x: -10, y: -10, width: frame.size.width/4, height: frame.size.width/4))
  </code></pre></li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/JYMxc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JYMxc.png" alt="enter image description here"></a></p>
  
  <p>I even tried setting <code>clipToBounds</code> to <code>false</code> but I get the same effect as pic # 3.</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
  
          cell.imageView.image = myImageArray[indexPath.row]
          cell.imageView.clipsToBounds = false
  
          return cell
      }
  </code></pre>
  
  <p>Where am I going wrong?</p>