title: UIButton inside a UIView subclass is not working, Swift
body: <p>I've seen similar SO questions, the most promising ones advised to set <code>.isUserInteractionEnabled = true</code> and <code>.bringSubview(toFront: subView)</code> however both these don't seem to work in my case (not sure if I am using it correctly). I am neither able to set the backgroundColor and border for the view.</p>
  
  <p>Please advise where I am going wrong.</p>
  
  <p>I would like to call a function on click of a editButton which is inside a subview.</p>
  
  <p>This is what I get when I run the code:</p>
  
  <p><a href="https://i.stack.imgur.com/ILMJR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ILMJR.jpg" alt="enter image description here"></a></p>
  
  <p>Here is the code:</p>
  
  <pre><code>class ProfilePhotoView: UIView{
  
      var profileImage    =   UIImageView()
      var editButton      =   UIButton()
      var currentViewController   : UIViewController
  
  
  
      init(frame: CGRect, viewController : UIViewController){
          self.currentViewController = viewController
  
          super.init(frame: frame)
          self.isUserInteractionEnabled = true
          setup()
      }
  
  
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
  
      func setup(){
  
          profileImage.image = #imageLiteral(resourceName: "profilePlaceHolder")
          editButton.setTitle("edit", for: .normal)
          editButton.setTitleColor(Colors.curieBlue, for: .normal)
          editButton.isUserInteractionEnabled = true
          editButton.addTarget(self, action: #selector(editPhoto), for: .touchUpInside)
  
          profileImage.translatesAutoresizingMaskIntoConstraints  =   false
  
          editButton.translatesAutoresizingMaskIntoConstraints     =   false
  
          self.addSubview(profileImage)
          self.addSubview(editButton)
  
  
          let viewsDict = [ "profileImage"    :   profileImage,
                            "editButton"       :   editButton
          ] as [String : Any]
  
          self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[profileImage]", options: [], metrics: nil, views: viewsDict))
          self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[profileImage]", options: [], metrics: nil, views: viewsDict))
  
  
  
          self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[editButton]", options: [], metrics: nil, views: viewsDict))
          self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[profileImage]-10-[editButton]", options: [], metrics: nil, views: viewsDict))
  
      }
  
      func editPhoto(){
          Utils.showSimpleAlertOnVC(targetVC: currentViewController, title: "Edit Button Clicked", message: "")
      }
  
  
  }
  </code></pre>
  
  <p>ViewController using ProfilePhotoView</p>
  
  <pre><code>class ProfilePhotoViewVC: UIViewController {
  
      var profilePhotoView    :   ProfilePhotoView!
      //let imagePickerController   =   UIImagePickerController() // Initializing imagePicker
      //var imagePickerDelegate     =   ProfilePhotoDelegate()
  
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let profilePhotoFrame               = CGRect(x: 0, y: 0, width: 300, height: 800)
          profilePhotoView                    =   ProfilePhotoView(frame: profilePhotoFrame, viewController: self)
          profilePhotoView.isOpaque           = false
          profilePhotoView.backgroundColor    = Colors.lightGrey
          profilePhotoView.layer.borderWidth  = 1
          profilePhotoView.layer.borderColor  = Colors.iOSBlue.cgColor
          profilePhotoView.translatesAutoresizingMaskIntoConstraints   =   false
  
          view.addSubview(profilePhotoView)
          view.bringSubview(toFront: profilePhotoView)
  
          view.isUserInteractionEnabled = true
  
          let viewsDict = [ "profilePhotoView"    :   profilePhotoView] as [String : Any]
  
          view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[profilePhotoView]", options: [], metrics: nil, views: viewsDict))
          view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[profilePhotoView]", options: [], metrics: nil, views: viewsDict))
  
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  }
  </code></pre>