Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iosdevie/c66d683656f44ff24a159be400e6cf27 to your computer and use it in GitHub Desktop.
Save iosdevie/c66d683656f44ff24a159be400e6cf27 to your computer and use it in GitHub Desktop.
display a ContextMenu when a user long-presses on a CollectionView cell
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil){ action in
let viewMenu = UIAction(title: "View", image: UIImage(systemName: "eye.fill"), identifier: UIAction.Identifier(rawValue: "view")) {_ in
print("button clicked..")
}
let rotate = UIAction(title: "Rotate", image: UIImage(systemName: "arrow.counterclockwise"), identifier: nil, state: .on, handler: {action in
print("rotate clicked.")
})
let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), identifier: nil, discoverabilityTitle: nil, attributes: .destructive, state: .on, handler: {action in
print("delete clicked.")
})
let editMenu = UIMenu(title: "Edit...", children: [rotate, delete])
return UIMenu(title: "Options", image: nil, identifier: nil, children: [viewMenu, editMenu])
}
return configuration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment