Show UIMenu without UIButton or UINavigationButton
users. I'm having an issue that I can't seem to figure out. I want to show a UIMenu when I press a row in my UICollectionView, so I've added a UIButton that spands edge-to-edge in each cell. When the button is pressed, a UIMenu appears. The problem is that the UICollectionView is not scroll-able with a UIButton in each cell. The system prioritises the UIButton over scrolling. I'd love to just have a UILabel and a menu showing when 'didSelectItemAt indexPath' is called, but UIMenus only work with UIButton and UINavigationBarButton.
If someone has a solution for me, please help out! Thank you very much, kind regards 😊
I suspect and I could be wrong, you have set delaysContentTouches
to false for the UICollectionView.
This would make any touch be interpreted as a button tap.
Instead of removing the button from the cell, here is my set up which sounds similar to yours and gives the desired end result:
Custom UICollectionView with a label and button that spans the whole collection view cell
class ButtonCollectionViewCell: UICollectionViewCell
{
static let reuseIdentifier = "ButtonCollectionViewCell"
let titleLabel = UILabel()
let hiddenButton = UIButton()
override init(frame: CGRect)
{
super.init(frame: frame)
contentView.backgroundColor = .yellow
configureLabel()
configureButton()
layoutIfNeeded()
}
required init?(coder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
private func configureLabel()
{
contentView.addSubview(titleLabel)
// Auto layout config to pin label to the edges of the content view
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
private func configureButton()
{
addSubview(hiddenButton)
// I add this red color so you can see the button takes up the whole cell
hiddenButton.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.75)
// Auto layout config to pin button to the edges of the content view
hiddenButton.translatesAutoresizingMaskIntoConstraints = false
hiddenButton.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
hiddenButton.topAnchor.constraint(equalTo: topAnchor).isActive = true
hiddenButton.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
hiddenButton.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
// Configure menu
hiddenButton.showsMenuAsPrimaryAction = true
hiddenButton.menu = UIMenu(title: "Select an option", children: [
UIAction(title: "Option 1") { action in
// do your work
},
UIAction(title: "Option 2") { action in
// do your work
},
])
}
}
In the ViewController
class UICollectionViewButtonCellViewController: UIViewController
{
private var collectionView: UICollectionView!
override func viewDidLoad()
{
super.viewDidLoad()
title = "Button Cell Example"
view.backgroundColor = .white
configureCollectionView()
}
private func configureCollectionView()
{
collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: createLayout())
collectionView.register(ButtonCollectionViewCell.self,
forCellWithReuseIdentifier: ButtonCollectionViewCell.reuseIdentifier)
collectionView.dataSource = self
// do not have the below line of code, by default delaysContentTouches is true
// collectionView.delaysContentTouches = false
view.addSubview(collectionView)
// Auto layout config to pin collection view to the edges of the view
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
}
private func createLayout() -> UICollectionViewFlowLayout
{
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let availableWidth = view.frame.width
let interItemSpacing: CGFloat = 5
// (available width - inter item spacing) / 2
let itemWidth = (availableWidth - interItemSpacing) / 2
layout.itemSize = CGSize(width: itemWidth, height: 100)
layout.minimumInteritemSpacing = interItemSpacing
layout.minimumLineSpacing = 5
return layout
}
}
extension UICollectionViewButtonCellViewController: UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int
{
return 20
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ButtonCollectionViewCell.reuseIdentifier,
for: indexPath) as! ButtonCollectionViewCell
cell.titleLabel.text = "Tap cell \(indexPath.item)"
return cell
}
}
This gives me the desired output of a scrollable UICollectionView with it's taps leading to a UIMenu interaction.