显示不带 UIButton 或 UINavigationButton 的 UIMenu

Joh*_*sen 5 xcode uibutton ios uicollectionview swift

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

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

Sha*_*ank 2

我怀疑,我可能是错的,您已将delaysContentTouchesUICollectionView 设置为 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
            },
        ])
    }
}
Run Code Online (Sandbox Code Playgroud)

在视图控制器中

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

Run Code Online (Sandbox Code Playgroud)

这为我提供了可滚动 UICollectionView 的所需输出,它的点击导致 UIMenu 交互。

UICollectionView 与自定义 UICollectionViewCell 显示 UIMenu UIContextMenuInteraction 从 UIButton 触发的交互

  • 哇,惊人的答案!非常感谢你,这真的帮助了我! (2认同)