skip to Main Content

I am trying to set a click event on my UIStackview to open a viewcontroller. When I do this nothing happens. It doesn’t even print the line when I tap it. I tried multiple solutions on this website, like UITapGestureRecognizer but to no avail. What am I doing wrong?

My code:

private let stackUIView: UIView = {
     let stackUIView = UIView(frame: CGRect(x: 25, y: 25, width: 400, height: 90))
    let tap = UIGestureRecognizer(target: self, action: #selector(stackviewClicked))
    stackUIView.addGestureRecognizer(tap)
    stackUIView.isUserInteractionEnabled = true
    
    return stackUIView
}()


@objc
    func stackviewClicked() {
        print("UIView Clicked")
        let testVC = testViewController()
        navigationController?.pushViewController(testVC, animated: true)

    }

2

Answers


  1. It should be UITapGestureRecognizer not UIGestureRecognizer

    let tap = UITapGestureRecognizer(target: self, action: #selector(stackviewClicked))
    
    Login or Signup to reply.
  2. For me works, thanks

    termsAndConditionsStack.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(stackviewClicked)))
    
    @objc func stackviewClicked() {
         print("works")
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search