skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search