9

I want darker background dim color for UIPopover. I know this can be achieved by subclassing UIPopoverBackgroundView as mentioned here but I am looking for any simpler way to do this.

P.S. I am using Objective C not Swift.

Community
  • 1
  • 1
Sasi
  • 1,666
  • 2
  • 24
  • 44

4 Answers4

15

Swift 4

I just came upon the same problem and found a solution similar to jimmyjudas.

In the viewController displayed as a popover:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.presentingViewController?.view.alpha = 0.3
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.presentingViewController?.view.alpha = 1
    }
Trevor
  • 1,075
  • 11
  • 15
  • 1
    Very cool and simple answer. I like it. It would be better with view animation: UIView.animate(withDuration: 0.5, animations: { self.presentingViewController?.view.alpha = 0.5 }) – mazend Feb 10 '20 at 11:22
3

Easiest method is to simply call self.view.alpha = 0.2 before presenting the popover and setting it back to 1.0 when the popover is dismissed.

  • Just to add a few more pointers to this answer, I overrode the ViewWillAppear and ViewWillDisappear in the UIViewController being displayed as the popover to set the alpha on the main view controller's view. (In Xamarin, I used `UIApplication.SharedApplication.KeyWindow.RootViewController.View.Alpha = 0.5f` with a few checks for nulls along the way.) – jimmyjudas Jan 28 '16 at 10:07
3

Since the user is asking for an Obj C solution - use in the VC that's being displayed in the popover

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.popoverPresentationController.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
}
Nostradamus
  • 1,497
  • 3
  • 20
  • 34
-1

UIPopover doesn't background property because it subclass of NSObject don't have customizing view property so you can change its content color -

 UIPopoverController *popC = [[UIPopoverController alloc] initWithContentViewController:TestingPC];
[[[popC contentViewController]  view] setBackgroundColor:[UIColor blackcolor]];
Nishant Gupta
  • 131
  • 15