SwiftyContextMenu icon indicating copy to clipboard operation
SwiftyContextMenu copied to clipboard

Context Menu not dismissing

Open syedrazackimran opened this issue 3 years ago • 3 comments

Thanks for your library.

I facing one critical issue. I using this library on the collection view. when the context menu presents, after tapping the menu actions, I am trying to push another controller or tapped outside to dismiss the context menu. still, the context menu presents on the window. refer the screenshots

private func setupContext(with indexPath: IndexPath) -> ContextMenu {
        
        let favoriteAction = ContextMenuAction(title: "Select Address", image: UIImage(named: "icon_context_select"),
                                               action: { [weak self] _ in
                                                guard let strongSelf = self else { return }
                                                strongSelf.selectAddressTapped(with: indexPath)
        })
        
        let shareAction = ContextMenuAction(title: "Edit", image: UIImage(named: "icon_context_edit"),
                                            action: { [weak self] _ in
                                                guard let strongSelf = self else { return }
                                                guard let cell = strongSelf.savedAddressCollectionView.cellForItem(at: indexPath) as? SavedAddressCollectionViewCell else { return }
                                                cell.removeContextMenu()
                                                cell.dismissContextMenu(completion: nil)
                                                strongSelf.editAddressTapped(with: indexPath)
        })
        
        let deleteAction = ContextMenuAction(title: "Delete", image: UIImage(named: "icon_context_delete"), tintColor: UIColor.red,
                                             action: { [weak self] _ in
                                                guard let strongSelf = self else { return }
                                                strongSelf.deleteAddressTapped(with: indexPath)
        })
        
        let actions = [favoriteAction, shareAction, deleteAction]
        let layout = ContextMenuLayout(width: 120, spacing: 10, padding: 0, sourceViewCornerRadius: 0)
        let animation = ContextMenuAnimation(sourceViewBounceRange: 0.9...1.0)
        let contextMenu = ContextMenu(title: nil, actions: actions, layout: layout, animation: animation, delegate: self)
        return contextMenu
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SavedAddressCollectionViewCell.identifier, for: indexPath) as!  SavedAddressCollectionViewCell
        cell.addContextMenu(setupContext(with: indexPath), for: .longPress(duration: 0.3), .tap(numberOfTaps: 1))
        return cell
    }

check the above code I add a context menu to the collectionView Cell.

Simulator Screen Shot - iPhone 11 Pro Max - 2020-09-07 at 19 41 44 Simulator Screen Shot - iPhone 11 Pro Max - 2020-09-07 at 19 41 46 Simulator Screen Shot - iPhone 11 Pro Max - 2020-09-07 at 19 41 52

check the above screen last one, I'm trying to dismiss on tap outside its not dismissing still it there. also if I push to another controller it still remains there.

syedrazackimran avatar Sep 07 '20 14:09 syedrazackimran

Hello there, can you try to call cell.dismissContextMenu(completion: nil) before cell.removeContextMenu()?

MarioIannotta avatar Sep 08 '20 06:09 MarioIannotta

yes, I did. still, issues remain there. kindly refer to the screenshots. It's not dismissing. after selecting the context menu action

cell.dismissContextMenu(completion: nil)
cell.removeContextMenu()

Simulator Screen Shot - iPhone SE (2nd generation) - 2020-09-09 at 11 14 43

syedrazackimran avatar Sep 09 '20 06:09 syedrazackimran

Sorry, i didn't really have the time to look into this issue until now. As a general rule, you shouldn't call removeContextMenu or dismissContextMenu within a ContextMenuAction closure. Everything should automatically be dismissed when the user taps one of the options.

Can you try to remove that or at least try to reproduce the issue using the demo project in this repository?

MarioIannotta avatar Sep 13 '20 14:09 MarioIannotta