如何更改UIPopoverPresentationController后面的叠加层的不透明度?

时间:2015-09-21 14:12:13

标签: ios uikit

我正在使用UIPopoverPresentationController在iOS应用中展示popovers。当它出现时,我想调暗popover背后的背景。我怎样才能做到这一点?在某个地方是否有API,或者当我呈现弹出窗口时,我是否必须在主视图上叠加某些内容?

6 个答案:

答案 0 :(得分:5)

通过UIPopoverPresentationController呈现并使用其委托方法更改containerView属性:

- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController {
//
popoverPresentationController.containerView.backgroundColor = [UIColor colorWithWhite:0 alpha:.72];

答案 1 :(得分:2)

插入式非侵入式解决方案:通过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()
            })
        }
    }

}

然后,当即将呈现一个popver时,将UIPopoverPresentationController的委托设置为PopoverDelegate的实例,并在弹出窗口后面获得一个暗淡的背景。

答案 2 :(得分:1)

当我呈现弹出窗口时,我最终通过在主视图上放置自己的叠加层来解决这个问题。我能够轻松地做到这一点,因为我通过一个单独的协调器类来完成我的所有弹出窗口,我的根视图控制器不是导航控制器,我可以轻松地到达所有地方。我还必须创建一个UINavigationController的子类,我在popover中提供了一个子类,因此我可以覆盖dismissViewControllerAnimated:completion:并通知协调器,以便删除覆盖(didDismiss委托方法不会被调用当弹出以编程方式解散时。)

答案 3 :(得分:1)

在swift 3中,您可以访问叠加层:

extension UIPopoverPresentationController {

    var dimmingView: UIView? {
       return value(forKey: "_dimmingView") as? UIView
    }
}

将控制器设置为弹出模式

controller.modalPresentationStyle = UIModalPresentationStyle.popover
controller.popoverPresentationController.dimmingView.backgroundColor = UIColor.black.withAlphaComponent(0.4)

答案 4 :(得分:0)

我刚刚创建了我的视图控制器,设置了弹出样式,然后呈现了它,并在完成回调中设置了背景颜色(因为那时containerView不会为nil)。与Swift 4+兼容。

let myViewController: MyViewController = MyViewController()
myViewController.modalPresentationStyle = UIModalPresentationStyle.popover

if let presentationController: UIPopoverPresentationController = myViewController.popoverPresentationController {
    presentationController.delegate = self.mainViewController
    presentationController.sourceView = sourceView
    presentationController.sourceRect = sourceRect
    self.mainViewController.present(myViewController, animated: false, completion: {
        presentationController.containerView?.backgroundColor = UIColor.white.withAlphaComponent(0.5)
    })
}

答案 5 :(得分:-3)

popoverController.contentViewController.view.alpha = 0.5;

Transparent UIPopover