Reputation: 781
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.
Upvotes: 5
Views: 4283
Reputation: 3405
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];
}
UPDATE: please use UIEditMenuInteraction
now as UIMenuController
is deprecated
Upvotes: 10