如何使放置在UICollectionView顶部的按钮对水龙头有反应?

时间:2019-04-25 19:12:05

标签: ios swift uicollectionview

充当画廊的UICollectionView

gallery.showsHorizontalScrollIndicator  = false
gallery.isPagingEnabled = true

放置在UIView容器内,并在此容器上添加了自定义按钮(收藏夹图像按钮):

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

问题是收藏夹按钮在点击时不起作用,为什么以及如何解决?我尝试了favBtn.becomeFirstResponder(),但没有成功。

2 个答案:

答案 0 :(得分:2)

将按钮置于最前面并启用交互

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

答案 1 :(得分:0)

该问题的原因是该按钮与具有isTranslucent = true属性的顶部导航栏重叠。一旦我设法将按钮移到导航栏之外,它将变为活动状态。