我怎样才能改变UIPopoverPresentationController背后的覆盖的不透明度?

我正在使用UIPopoverPresentationController在iOS应用中呈现popup窗口。 当它出现时,我想调暗背后的背景。 我怎样才能做到这一点? 有没有一个API的地方,或者当我提出popover时,我将不得不覆盖主视图上的东西?

当我介绍popover时,我最终解决了这个问题。 我能够轻松地做到这一点,因为我通过一个单例协调器类来完成所有的popup工作,我的根视图控制器不是一个导航控制器,我可以在任何地方轻松地实现。 我还必须创build一个UINavigationController的子类,我在dismissViewControllerAnimated:completion:提供了一个子类,所以我可以重写dismissViewControllerAnimated:completion:并通知协调器,这样就可以删除叠加层(当popover以编程方式解除时,didDismiss委托方法不会被调用) 。

一个非侵入式解决scheme:通过UIPopoverPresentationControllerDelegate显示/隐藏覆盖视图,如下所示:(在Swift 2.0中)

 class PopoverDelegate: NSObject, UIPopoverPresentationControllerDelegate { var overlay: UIView? dynamic func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { return .None } dynamic func presentationController(presentationController: UIPresentationController, willPresentWithAdaptiveStyle style: UIModalPresentationStyle, transitionCoordinator: UIViewControllerTransitionCoordinator?) { // add a semi-transparent view to parent view when presenting the popover let parentView = presentationController.presentingViewController.view let overlay = UIView(frame: parentView.bounds) overlay.backgroundColor = UIColor(white: 0.0, alpha: 0.4) parentView.addSubview(overlay) let views: [String: UIView] = ["parentView": parentView, "overlay": overlay] parentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[overlay]|", options: [], metrics: nil, views: views)) parentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[overlay]|", options: [], metrics: nil, views: views)) overlay.alpha = 0.0 transitionCoordinator?.animateAlongsideTransition({ _ in overlay.alpha = 1.0 }, completion: nil) self.overlay = overlay } deinit { // remove the overlay with animation. // the delegate method popoverPresentationControllerDidDismissPopover(_:) is not called // if the popover is dismissed programmatically, // so the removal goes here. guard let overlay = overlay else { return } dispatch_async(dispatch_get_main_queue()) { UIView.animateWithDuration(0.2, animations: { overlay.alpha = 0.0 }, completion: { _ in overlay.removeFromSuperview() }) } } } 

然后,当一个popper即将被提交时,将UIPopoverPresentationController的委托设置为一个UIPopoverPresentationController的实例,并在PopoverDelegate后面得到一个灰色的背景。

 popoverController.contentViewController.view.alpha = 0.5; 

透明的UIPopover