UITapGestureRecognizer on UICollectionView header not working?

1.4k views Asked by At

I am Trying to add a Tap Gesture Recognizer to the header of my UICollection view, but no matter what, I can't get the numberOfPostsViewTapped() function to fire off. I've been trying for hours, and have tried using other UI elements such as other views or labels in the header view, but nothing is helping. Some guidance would be much appreciated.

 func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader: // only checking header - no footer on this view

        // use an external class for the header UICollectionViewCell in order to set outlets on a non-reusable cell
        // if you try to set outlets on a reusable cell, such as a header, it will fail
        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! ProfileCollectionViewHeader

        // dynamically set user profile information
        headerView.usernameTextLabel.text = user?.name
        headerView.numberOfPostsTextLabel.text = user?.numberOfPosts != nil ? "\(user!.numberOfPosts!)" : "0"

        let numberOfPostsViewSelector : Selector = #selector(self.numberOfPostsViewTapped)
        let viewPostsViewGesture = UITapGestureRecognizer(target: self, action: numberOfPostsViewSelector)
        viewPostsViewGesture.numberOfTapsRequired = 1
        viewPostsViewGesture.delaysTouchesBegan = true
        headerView.numberOfPostsTextLabel.userInteractionEnabled = true;
        headerView.numberOfPostsTextLabel.addGestureRecognizer(viewPostsViewGesture)

        return headerView

    default:

        assert(false, "Unexpected element kind")

    }
}
func numberOfPostsViewTapped(sender: UITapGestureRecognizer){
    print("HErE")
}
3

There are 3 answers

4
Edwin Finch On

Check your frames. Is your UICollectionView within its frame?

If a view is out of its frame, and doesn't clip to its bounds, it will show the view's contents, though user interaction will not work with it.

1
Christian Abella On

I think you need to set userInteractionEnabled to true to your headerView so that the tapping would reach your label which is a child of your headerView.

headerView.userInteractionEnabled = true
0
Munib Hamza On

Made few changes to your code and this worked for me

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        switch kind {
        case UICollectionView.elementKindSectionHeader:
            guard
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "AddLinksReusableView", for: indexPath) as? AddLinksReusableView else {
                fatalError("Invalid view type")
            }
            let numberOfPostsViewSelector : Selector = #selector(self.imgVuTapped)
            let viewPostsViewGesture = UITapGestureRecognizer(target: self, action: numberOfPostsViewSelector)
            headerView.profileImg.isUserInteractionEnabled = true

            viewPostsViewGesture.numberOfTapsRequired = 1
            viewPostsViewGesture.delaysTouchesBegan = true
            headerView.profileImg.addGestureRecognizer(viewPostsViewGesture)
            return headerView
        default:
            assert(false, "Invalid element type")
        }
    }
    @objc func imgVuTapped (sender: UITapGestureRecognizer){
        print("HErE, Its working")
    }

Happy Codding