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

时间:2017-06-14 22:39:42

标签: ios swift uicollectionview uimenucontroller

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

我从网上看了很多引用,但没有一个能让它起作用,这里是代码:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

基本上,移动这些方法:

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

  • (BOOL)canBecomeFirstResponder {

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

有了这个,我就可以看到自定义菜单了!