How can I make my cells selectable when there is an UIView below the UICollectionView?

ⅰ亾dé卋堺 提交于 2019-12-11 08:39:48

问题


I have a collection view, and added a UIView below it. The cells shows up on top of it, but they aren't selectable. How can I make them selectable again?

What I have tried so far:

UIView has zPosition -1 and my cells have +1 UIView.isUserInteractionEnabled = true

I have tried to add the UIView both as view.addSubview and collectionView?.addSubview

collectionView?.insertSubview(UIView, belowSubview: collectionView!)

This guy has the same problem, with no solution provided: Inserting Subview Below UICollectionView

Edit: I'm using this code to determine what cells are selected

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    //Code here...
}

It all works when there is no UIView below. But I want to have a visible "container" for my collectionview content insets, so I created an UIView. Problem is the above code doesn't get fired at all when clicking on the cells with the UIView below.


回答1:


It's happening because your other view is covering the collection view.

Use view.addSubview first and then call view.bringSubview(toFront: collectionView!)




回答2:


Why dont you do the selection manually at the moment when the view is tapped:

func selectItem(at: IndexPath?, animated: Bool, scrollPosition: UICollectionViewScrollPosition)

Can you show more in code?



来源:https://stackoverflow.com/questions/44906854/how-can-i-make-my-cells-selectable-when-there-is-an-uiview-below-the-uicollectio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!