如何使UIPopoverPresentationController在iOS 9中半透明

4
我使用IB创建了一个segue来呈现另一个视图的弹出窗口。
我在prepareForSegue中添加了代码,将UIPopoverPresentationControllerDelegate委托给初始控制器。
然后我设置了演示风格:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController,
     traitCollection: UITraitCollection) -> UIModalPresentationStyle {
      return UIModalPresentationStyle.None
}

这给我带来了一个不错的标准弹出窗口。

但是,我想要一个半透明的弹出窗口。

我尝试了几件事情:

  • 我在IB中将背景颜色设置为“clear”
  • 我试图在弹出窗口视图上设置alpha值
3个回答

7
要使一个视图控制器在另一个视图控制器上方并具有透明度,您需要返回UIModalPresentationStyle.OverCurrentContext

哦...还有一件事,您需要设置容器视图的背景色为带有透明度的颜色,并且在容器视图内部托管的视图也是如此。 - Victor Ronin

1
概念:在弹出窗口之前调整源视图控制器的 alpha 值,并在关闭弹出窗口时将其恢复为 1.0:
  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;
    }
    

0

我无法通过已发布的答案实现所需的效果。然而,这两行代码对我很有帮助:

popupVC.modalPresentationStyle = UIModalPresentationStyle.none
popupVC.popoverPresentationController?.backgroundColor = UIColor.clear // This needs to be set for the translucent appearance

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接