Ivan I
Ivan I

Reputation: 9990

UIContextualAction display UIMenu

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.

Upvotes: 3

Views: 759

Answers (1)

Tyler927
Tyler927

Reputation: 196

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)
                    }
                }
            }
        }
    }
}

Upvotes: 2

Related Questions