Failed to find a presenting view controller UIContextMenuConfiguration

607 views Asked by At

I have a parent that presents a child view controller.

        view.addSubview(commentController.view)
        
        addChild(commentController)
        commentController.didMove(toParent: self)

Really basic stuff and everything works well.

Until I implemented

func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? 

Whenever I long-press a cell, which should present a UIMenu, I receive this error: Failed to find a presenting view controller for view. The interaction's view (or an ancestor) must have an associated view controller for presentation to work

I've never seen this error before, and I can't find it anywhere on line.

It doesn't look like I have any delegate method to direct the presentation to a specific view controller.

For example if I wanted to present a modal on top of this child I could do something like:

parent?.present(vc, animated: true)

I'm not sure how to fix this.

So to sum up this issue:

I have a child view controller with a UITableView. I'm trying to use the delegate method for presenting a UIMenu. It won't work, and I assume because it is a child view controller, but no idea why or how to fix it

1

There are 1 answers

0
DBSoft On

I was getting the same error, I am not sure if this is exactly the same situation as you or not, but here is a description of my situation and how I fixed it:

The problem was I was creating the UIWindow manually but I was not using the view returned by UIViewController's "view" property, and then hiding the UITransitionView since it was blocking the rest of the content, therefore the content, including the UINavigationBar and the UITableView that had menus attached to them were not direct descendants of the UIViewController, thus when the system traversed the hierarchy up, it did not find a UIViewController to present the menu, even though there was one in a different branch of the hierarchy of the UIWindow.

The fix is once the UIWindow is created with the UIViewController in the rootViewController, we query it's "view" property, which will trigger the creation of the UITransitionView hierarchy and since we aren't using nibs it will create a UIView taking up the entire content area. There we can attach the UINavigationBar and any other content, which will not be obstructed and be descendants of the UIViewController so it can be found and avoiding the error in the title of this question.