问题
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