5

Whatsapp screenshot

I would like to know how to implement this "context menu" with action buttons like the one that appears on whatsapp and other apps when you click message.

Thank you very much.

Amal T S
  • 3,327
  • 2
  • 24
  • 57
Eironeia
  • 771
  • 8
  • 20
  • 1
    create your custom UIView as you want and add gesture on your view and when you tap on your view find tap location and appear your custom view. – Rajeev Singh Jul 06 '17 at 05:44
  • Check this: [Tutorial](https://www.ioscreator.com/tutorials/use-context-menu-table-view-tutorial) – aashish tamsya Jul 06 '17 at 05:45

1 Answers1

9

That is UIMenuController.

Swift

    let contextMenu = UIMenuController.shared
    contextMenu.menuItems = [
    UIMenuItem(title: "test", action: #selector(testclicked)),
    UIMenuItem(title: "test 1", action: #selector(test1clicked)),
    UIMenuItem(title: "test2", action: #selector(test2clicked)),
    UIMenuItem(title: "test3", action: #selector(test3clicked))
    ]
    contextMenu.showMenu(from: self.view, rect: CGRect(x: 100, y: 190, width: 100, height: 20))

Objective C

- (void)showMenu
{
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems = @[
       [[UIMenuItem alloc] initWithTitle:@"Title1" action:@selector(MyAction1)],
       [[UIMenuItem alloc] initWithTitle:@"Title2" action:@selector(MyAction2)],
       [[UIMenuItem alloc] initWithTitle:@"Title3" action:@selector(MyAction)]];
    [menu setTargetRect:self.bounds inView:self];
    [menu setMenuVisible:YES animated:YES];
}
Amal T S
  • 3,327
  • 2
  • 24
  • 57
  • 1
    Note that `UIMenuController` is deprecated as of iOS 16.0. `UIEditMenuInteraction` is the general solution since iOS 16.0. – HangarRash Aug 02 '23 at 05:32