I am showing a complicated controller AskController (descended from UIViewController, UIPopoverPresentationControllerDelegate and UIPopoverControllerDelegate) over some different controllers, popover style.
Is there an easy way to automatically dim and blur whatever happens behind said AskController, and remove those effects on a dismiss of AskController in any way?
Cannot make AskController to be a descendant of UIPopoverPresentationController instead.
The effect has to work even if the device changed orientation multiple times.
Already tried setting popoverPresentationController.backgroundColor – does nothing.
This is how I’m presenting it, if relevant:
let windowAsking = AskController()
windowAsking.modalPresentationStyle = .popover
if let popoverController = windowAsking.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = centerOfTheScreen()
popoverController.permittedArrowDirections = []
popoverController.delegate = windowAsking
}
self.present(windowAsking, animated: true)
2
Answers
After some trial-and-error I discovered a way:
And when showing the controller:
main here being the controller that AskController is being shown over.
But this way is awkward because of the need to assign main on opening the controller, so I would appreciate a better, more elegant way.
Here is how I’ve handled that:
Make the view controller a full-screen modal. Use a full screen blur visual effects view as the background. Then build a view hierarchy that looks like your modal/popover and make it a subview of your blur view. Add a tap gesture recognizer to your blur view that dismisses the modal.