KevinB
KevinB

Reputation: 2484

Swift - addTarget() not working with addSubview()

I'm trying to add a banner that appear from the bottom:

enter image description here

So I created a UIView with a UIButton inside. The problem is that when I click on the button, nothing happen.

@objc func myButtonAction() {
      print("My Button tapped")
}

func notifBanner(message: String, color: UIColor, backgroundColor: UIColor, button: UIButton){

      let appDelegate = UIApplication.shared.delegate as! AppDelegate
      let window = appDelegate.window!

      if infoViewIsShowing == false{
         infoViewIsShowing = true

         let grey1 = UIColor(red: 196/255, green: 196/255, blue: 196/255, alpha: 1)

         let infoViewHeight = CGFloat(50)
         let infoViewY = window.bounds.height + infoViewHeight

         let infoView = UIView(frame: CGRect(x: 10, y: infoViewY, width: window.bounds.width - 20, height: infoViewHeight))
         infoView.backgroundColor = backgroundColor
         infoView.layer.cornerRadius = 8
         infoView.clipsToBounds = true

         infoView.isUserInteractionEnabled = true
         window.addSubview(infoView)


         infoView.alpha = 0.4

         let widthButton = CGFloat(115)
         let goToProfileButton = UIButton()
         goToProfileButton.frame = CGRect(x: 0, y: 0, width: infoLabelWidth, height: infoLabelHeight)
         goToProfileButton.setTitle("View Profile", for: .normal)
         goToProfileButton.tintColor = .white
         goToProfileButton.addTarget(self, action: #selector(self.myButtonAction), for: .touchUpInside)
         goToProfileButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)

         infoView.addSubview(goToProfileButton)

         // Animate bannerView
         UIView.animate(withDuration: 0.4, animations: {

            infoView.alpha = 1
            infoView.frame.origin.y = window.bounds.height - infoViewHeight - 60

         }, completion: { (finished: Bool) in
            if finished{

               UIView.animate(withDuration: 0.4, delay: 3, options: .curveEaseIn, animations: {
                  // move up
                  infoView.frame.origin.y = infoViewY
                  infoView.alpha = 0.3

               }, completion: { (finished: Bool) in
                  if finished {
                     infoView.removeFromSuperview()
                     self.infoViewIsShowing = false
                  }
               })

            }
         })
      }
   }

I tried to add infoView.isUserInteractionEnabled = true but it's still not working. Do you have any idea about how I could send the action when I click on the button?

Upvotes: 0

Views: 238

Answers (2)

rmaddy
rmaddy

Reputation: 318774

Assuming you wish to be able to tap the button during the 3 seconds you wish the button to appear, you could try changing the code to use DispatchQueue asyncAfter.

UIView.animate(withDuration: 0.4, animations: {
    infoView.alpha = 1
    infoView.frame.origin.y = window.bounds.height - infoViewHeight - 60
}, completion: { (finished: Bool) in
    if finished{
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            UIView.animate(withDuration: 0.4, delay: 0, options: .curveEaseIn, animations: {
                // move up
                infoView.frame.origin.y = infoViewY
                infoView.alpha = 0.3
            }, completion: { (finished: Bool) in
                if finished {
                    infoView.removeFromSuperview()
                    self.infoViewIsShowing = false
                }
            })
        }
    }
})

Upvotes: 2

Mohammed Alsamak
Mohammed Alsamak

Reputation: 1

you need to add UITapGestureRecognizer it will work for you

   let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
myView.addGestureRecognizer(tap)

@objc func handleTap(sender: UITapGestureRecognizer? = nil) {
    // handling code
}

Upvotes: -1

Related Questions