显示不带 UIButton 或 UINavigationButton 的 UIMenu

Show UIMenu without UIButton or UINavigationButton

用户。 我遇到了一个我似乎无法弄清楚的问题。我想在我的 UICollectionView 中按一行时显示一个 UIMenu,因此我添加了一个 UIButton,它在每个单元格中从边到边跨越。按下按钮时,会出现一个 UIMenu。问题是 UICollectionView 不能滚动,每个单元格中都有一个 UIButton。系统优先考虑 UIButton 而不是滚动。当 'didSelectItemAt indexPath' 被调用时,我希望只有一个 UILabel 和一个显示菜单,但 UIMenus 只能与 UIButton 和 UINavigationBarButton 一起使用。

如果有人对我有解决方案,请帮忙! 非常感谢你, 亲切的问候

我怀疑而且我可能是错的,您已将 UICollectionView 的 delaysContentTouches 设置为 false。

这将使任何触摸都被解释为点击按钮。

这里不是从单元格中删除按钮,而是我的设置,它听起来与您的类似,并给出了所需的最终结果:

带有跨越整个集合视图单元格的标签和按钮的自定义 UICollectionView

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
            },
        ])
    }
}

在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
    }
}

这给了我一个可滚动的 UICollectionView 的期望输出,它的点击导致了 UIMenu 交互。