confirmationDialog not working in child view controller

So actionSheet is deprecated in favor of confirmationDialog. When trying to migrate I'm noticing that the latter doesn't work when the view is in a child view controller!

I have created a simple project to demonstrate the issue. https://github.com/gbuela/cddemo

I have a SheetView and a DialogView, they display a button to try either API.

In the ViewController, these are loaded into UIHostingController's and added as children into two UIView's. Only SheetView works.

I added a Pop Up button to alternatively load DialogView in a pop up, in this context it is not a child view controller and it works.

Is this simply a SwiftUI bug or do I need to do something else to make it work?

Answered by Levivig in 713794022

I had the same issue. The problem was with adding the child controller, or rather the lack of it.

The solution was to call addChild(_:) and didMove(toParent:) when adding the UIHostingController.

I updated your example:

let dialogView = DialogView()
let dialogVC = UIHostingController(rootView: dialogView)

addChild(dialogVC)

bottomView.addPinnedSubview(child: dialogVC.view)

dialogVC.didMove(toParent: self)

Hope this helps!

I'm having the same issue. Did you find a workaround?

Accepted Answer

I had the same issue. The problem was with adding the child controller, or rather the lack of it.

The solution was to call addChild(_:) and didMove(toParent:) when adding the UIHostingController.

I updated your example:

let dialogView = DialogView()
let dialogVC = UIHostingController(rootView: dialogView)

addChild(dialogVC)

bottomView.addPinnedSubview(child: dialogVC.view)

dialogVC.didMove(toParent: self)

Hope this helps!

confirmationDialog not working in child view controller
 
 
Q