Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UIPopoverPresentationController semi transparent for iOS 9

I used IB to create a segue to present another view a popover.

I added a code in prepareForSegue to deletage UIPopoverPresentationControllerDelegate to initial controller.

And I set presentation style:

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController,
     traitCollection: UITraitCollection) -> UIModalPresentationStyle {
      return UIModalPresentationStyle.None
}

This gives me a nice standard popover.

However, I want to make a semi-transparent popover.

I tried couple of things:

  • I set background color in IB to "clear"
  • I tried to set a alpha on the popover view
like image 893
Victor Ronin Avatar asked Feb 09 '23 14:02

Victor Ronin


2 Answers

To have a view controller on top of another with transparency, you'll need to return UIModalPresentationStyle.OverCurrentContext.

like image 194
bsarrazin Avatar answered Feb 13 '23 03:02

bsarrazin


Concept: adjust the alpha value of the source view controller before segueing to the popover, and back to 1.0 again when it has been dismissed:

  1. Set the source view controller as a popover delegate

    class MyVC: UIViewController, UIPopoverPresentationControllerDelegate {
    
  2. Set the delegate and source view alpha in the 'prepare for segue' function (on the way to the popover)

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let controller = segue.destination as! ISACGlossaryTVC
        controller.popoverPresentationController!.delegate = self
        self.view.alpha = 0.2;
    }
    
  3. Create delegate method popoverPresentationControllerDidDismissPopover , and reset the source view alpha back when the pop-over has been dismissed

    func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
        self.view.alpha = 1.0;
    }
    
like image 39
Elardus Erasmus Avatar answered Feb 13 '23 04:02

Elardus Erasmus