0
votes

I want to add a tap gesture recognizer to a custom UIView (which represent an Image and a Label). It seems that the gesture recognizer is not added to the view or that the subviews are not considered as the UIView itself, hence not working.

Here is how I add my view :

  • Navbar.swift :
let indexSubview = IconTextView(svgName: "https://placeholder.pics/svg/30", textKey: "Index")
self.indexButton.addSubview(indexSubview)
let indexButtonTap = UITapGestureRecognizer(target: self, action: #selector(goToIndex))
indexButton.addGestureRecognizer(indexButtonTap)

(IconTextView being my custom view)

Then when I tap the indexButtonnothing is working.

My tap function, just in case:

@objc func goToIndex(sender:UITapGestureRecognizer) {
        print("GO TO INDEX")
        router.setRoute(routeName: "INDEX", routeParam: "")
    }

I don't understand why it is not working, the userInteractions are enabled on all the elements.

1

1 Answers

0
votes

Your precedent code + tap gesture, I edit constraints, add containerView:

class Aiutotipo: UIViewController {

let myImageView: UIImageView = {
    
    let iv = UIImageView()
    iv.contentMode = .scaleToFill
    iv.clipsToBounds = true
    iv.backgroundColor = .red
    iv.translatesAutoresizingMaskIntoConstraints = false
    
    return iv
}()

let myLabel: UILabel = {
    let label = UILabel()
    label.text = "Débats"
    label.textColor = .white
    label.textAlignment = .center
    label.font = .systemFont(ofSize: 30, weight: .semibold)
    label.translatesAutoresizingMaskIntoConstraints = false
    
    return label
}()

let containerView = UIView() //your container view

override func viewDidLoad() {
    super.viewDidLoad()
    
    containerView.backgroundColor = .red // red bacground for container view visible, set .clear for transparent bg
    containerView.isUserInteractionEnabled = true
    containerView.translatesAutoresizingMaskIntoConstraints = false
    
    let indexButtonTap = UITapGestureRecognizer(target: self, action: #selector(goToIndex))
    containerView.addGestureRecognizer(indexButtonTap)
    
    myImageView.image = UIImage(named: "profilo") // set here your image
    
    let myWidth = myLabel.intrinsicContentSize.width // This reveal only text width in label
    
    view.addSubview(containerView)
    containerView.heightAnchor.constraint(equalToConstant: myWidth + 50).isActive = true
    containerView.widthAnchor.constraint(equalToConstant: myWidth).isActive = true
    containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    
    containerView.addSubview(myImageView)
    myImageView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
    myImageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
    myImageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
    myImageView.heightAnchor.constraint(equalTo: myImageView.widthAnchor).isActive = true
    
    containerView.addSubview(myLabel)
    myLabel.topAnchor.constraint(equalTo: myImageView.bottomAnchor).isActive = true
    myLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
    myLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
    myLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
}

@objc fileprivate func goToIndex() {
    print("GO TO INDEX")
 }
}

This is the result

enter image description here