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

问题描述:

我试图在UICollectionViewController子类上使用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

任何想法?

+0

遇到类似的问题;你有没有找到解决方案的运气? –

欧米尔 - 检查出此链接:http://dev.glide.me/2013/05/custom-item-in-uimenucontroller-of.html

基本上,移动这些方法:

  • (BOOL)canPerformAction:(SEL)动作withSender:(ID)发送方{

  • (BOOL)canBecomeFirstResponder {

...收集到nView细胞子类的作品。然后您必须将此选择器重新传递给单元格代理。

有了这个,我能够让我的自定义菜单出现!