UIMenuController 未在 UICollectionViewController 子类上显示自定义操作



我正在尝试在UICollectionViewController子类上使用UIMenuController显示自定义操作,即使剪切、复制和粘贴操作按预期显示,由于某种原因,我的自定义操作没有。

我遵循了很多来自网络的参考资料,但没有一个能让它工作,这里是代码:

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    fileprivate var items = [MyClass]()
    // MARK: - UICollectionViewDataSource
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath)
        /* update cell properties */
        return cell
    }
    // MARK: - UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: itemSize, height: itemSize)
    }
    override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return true
    }
    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
        /* Do Something */
    }
    override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    public func menuAction(_ sender: UIMenuItem) {
        /* Action method*/
    }
}

已尝试按如下方式添加菜单项:

        let menuItem = UIMenuItem(title: SFLocalization.localizedString("Common-remove"), action: #selector(CollectionViewController.menuAction(_:)))
        let menuController = UIMenuController.shared
//        menuController.menuItems?.append(menuItem)
        menuController.menuItems = [menuItem]

viewDidLoadcollectionView(_ collectionView:, shouldShowMenuForItemAt) -> Bool

有什么想法吗?

Omer - 查看此链接:http://dev.glide.me/2013/05/custom-item-in-uimenucontroller-of.html

基本上,移动这些方法:

  • (BOOL(canPerformAction:(SEL(action withSender:(id(sender {

  • (BOOL(canBe成为第一响应者 {

。到集合视图单元格子类工作。然后,您必须将此选择器传递回单元格委托。

有了这个,我能够显示我的自定义菜单!

最新更新