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)
After some trial-and-error I discovered a way:
lazy var blackerOutOfPreviousScreen: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: .systemUltraThinMaterialDark)
let blackerOut = UIVisualEffectView(effect: blurEffect)
blackerOut.backgroundColor = UIColor.black.withAlphaComponent(0.1)
return blackerOut
}()
And when showing the controller:
main?.view.addSubview(blackerOutOfPreviousScreen)
<insert anchoring to the edges of the screen>
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.