How can I change the opacity of the overlay behind UIPopoverPresentationController?

后端 未结 6 2016
南笙
南笙 2021-01-12 19:28

I\'m using UIPopoverPresentationController to present popovers in an iOS app. I want to dim the background behind the popover when it comes up. How can I do thi

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 19:52

    I just created my view controller, set the popover style and then presented it, setting the background color in the completion callback (because the containerView will be non-nil at that point). Works well with 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)
        })
    }
    

提交回复
热议问题