Hi Keeganrush - Thank you for the tutorial about ContextMenu. It is very clear and easy to implement. But I need to show different customize preview and actions in a single tableViewCell interaction. As an example, if the user taps only the image in the cell, the actions should be like save, edit and share and if the user taps the cell, then the options should be Map and share. This is a very similar kind of UI in the Twitter Application. Awaiting for your reply.
Hi @pin2karthik, I’m glad you enjoyed the tutorial! It sounds like you want to add a UIContextMenuInteraction to different views in your cell, not the whole cell itself. For that, you can’t use the UITableViewDelegate as a convenient way to add context menus. Rather, try add the context menus directly to your UITableViewCell, using the process outlined in the tutorial:
Add a UIContextMenuInteraction to a view (in this case, the cell’s image view or other view).
Implement contextMenuInteraction(_:configurationForMenuAtLocation:), the one required method of UIContextMenuInteractionDelegate.
Build a UIContextMenuConfiguration with all your menu items for each individual view that should have a context menu.
I’ve implemented a context menu according to this tutorial in my app. UIkit, no storyboards, all programmatic. The menu pops up when holding on a button. Ity works and looks fine, BUT in my console i get an constraints warning: (
*Will attempt to recover by breaking constraint * <NSLayoutConstraint:0x6000022a3cf0 groupView.actionsSequence…height >= 66 (active, names: groupView.actionsSequence…:0x7f8d7f8b6600 )>
This groupView is not a view of mine. I suspect I need to set translatesAutoresizingMaskIntoConstraints to the context menu somehow, somewhere, but I have no idea how?
My code in the viewcontroller:
let interaction = UIContextMenuInteraction(delegate: self)
annotationTypeButton.addInteraction(interaction)
And the extension
//MARK: - UIContextMenuInteractionDelegate
extension AnnotationDetailsViewController: UIContextMenuInteractionDelegate {
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: "annotationTypeMenu" as NSCopying, previewProvider: nil) { _ in
let children: [UIMenuElement] = self.makeAnnotationTypeActions()
return UIMenu(title: "", children: children)
}
}
func makeAnnotationTypeActions() -> [UIAction] {
var actions = [UIAction]()
for type in AnnotationType.allCases {
actions.append( UIAction(title: type.rawValue, image: type.image, identifier: nil, attributes: []) { _ in
let annotationType = AnnotationType(rawValue: type.rawValue) ?? AnnotationType.tips
self.annotation.type = annotationType
self.configureAnnotationTypeButton(with: annotationType)
})
}
return actions
}
}