how to make button placed on top of UICollectionView react to the tap?

Refresh

April 2019

Views

13 time

-1

UICollectionView which behaves as a gallery

gallery.showsHorizontalScrollIndicator  = false
gallery.isPagingEnabled = true

is placed inside UIView container and on this container, custom button (favorite image button) is added:

enter image description here

galleryContainer.addSubview(gallery)
galleryContainer.addSubview(pageControl)

...

galleryContainer.addSubview(favBtn)
favBtn.topAnchor.constraint(equalTo: galleryContainer.topAnchor, constant: 40).isActive = true
favBtn.trailingAnchor.constraint(equalTo: galleryContainer.trailingAnchor, constant: -30).isActive = true
favBtn.heightAnchor.constraint(equalToConstant: 30).isActive = true
favBtn.widthAnchor.constraint(equalToConstant: 30).isActive = true

The problem is that the favorite button does not react on the tap, why, and how can this be fixed? I tried favBtn.becomeFirstResponder() but without any success.

1 answers

1

Bring your button to the front and enable interact

galleryContainer.bringSubview(toFront: favBtn)
favBtn.isUserInteractionEnabled = true