title: How to center UIButton programmatically in UIImageView?
body: <p>I'm creating <code>ViewCell</code> programmatically and I want to center <code>UIButton</code> to <code>UIImageView</code>, so it would look like this:</p>
  
  <pre><code> -----------------------------
  |                             |
  |                             |
  |            Button           |
  |                             |
  |                             |
   -----------------------------
  </code></pre>
  
  <p>To achieve that first in init method I create <code>UIImageView</code> and <code>UIButton</code>:</p>
  
  <pre><code>let imageView = UIImageView()
  imageView.contentMode = .scaleAspectFill
  imageView.layer.masksToBounds = true
  imageView.isUserInteractionEnabled = true
  
  let button = UIButton(type: .custom)
  button.setImage(UIImage(named: "button_image"), for: .normal)
  button.contentMode = .center
  button.frame = CGRect(x: 0, y: 0, width: 42, height: 42)
  </code></pre>
  
  <p>And then I add some constraints, in case of button and imageview I added these constraints:</p>
  
  <pre><code>button.addConstraint(NSLayoutConstraint(item: button, attribute: .centerX, relatedBy: .equal, toItem: imageView, attribute: .centerX, multiplier: 1, constant: 0))
  button.addConstraint(NSLayoutConstraint(item: button, attribute: .centerY, relatedBy: .equal, toItem: imageView, attribute: .centerY, multiplier: 1, constant: 0))
  </code></pre>
  
  <p>But as result I always crash my app. </p>
  
  <p>How can I achieve centered button?</p>