2

I've noted that in the Facebook Messenger they display the UIMenu when someone presses the UIContextualAction.

I might be missing something, but I don't see how to do it. If someone knows the code to do this, I would highly appreciate it.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57

1 Answers1

2

Okay so I don't love this method, but so far this has worked for me. I found this solution to access the UIButton, which you can then set a menu for.

func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
    for subview in tableView.subviews {
        if NSStringFromClass(type(of: subview)) == "_UITableViewCellSwipeContainerView" {
            for swipeContainerSubview in subview.subviews {
                if NSStringFromClass(type(of: swipeContainerSubview)) == "UISwipeActionPullView" {
                    for case let button as UIButton in swipeContainerSubview.subviews {
                        var menuItems = [UIAction]()
                        menuItems.append(UIAction(title: "Test") { action in
                            print("Test")
                        })
                        button.showsMenuAsPrimaryAction = true
                        button.menu = UIMenu(title: "", children: menuItems)
                    }
                }
            }
        }
    }
}
Tyler927
  • 197
  • 1
  • 12
  • 2
    Just to note that it seems that this solution has slightly regressed in iOS 16 - if you invoke the menu and dismiss it by clicking outside of it and then try to open the menu on the same item it won't work. But it does work on any other item or if something from the menu is picked previously. – Ivan Ičin Sep 08 '22 at 23:16