【问题标题】:How to make UIPopoverPresentationController semi transparent for iOS 9如何使 iOS 9 的 UIPopoverPresentationController 半透明
【发布时间】:2015-11-12 20:15:06
【问题描述】:

我使用 IB 创建了一个 segue 来呈现另一个视图一个弹出框。

我在prepareForSegue 中添加了一个代码来删除 UIPopoverPresentationControllerDelegate 到初始控制器。

我设置了演示风格:

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController,
     traitCollection: UITraitCollection) -> UIModalPresentationStyle {
      return UIModalPresentationStyle.None
}

这给了我一个不错的标准弹出框。

但是,我想制作一个半透明的弹出框。

我尝试了几件事:

  • 我将 IB 中的背景颜色设置为“清除”
  • 我尝试在弹出视图上设置 Alpha

【问题讨论】:

    标签: ios swift ios9 popover


    【解决方案1】:

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

    【讨论】:

    • 哦...还有一件事,您需要使用一些具有 alpha 的颜色来设置 Container View 的背景,对于托管在 Container 视图内部的视图也是如此。
    【解决方案2】:

    概念:在切换到弹出框之前调整源视图控制器的 alpha 值,当它被关闭时再次回到 1.0:

    1. 将源视图控制器设置为弹出框委托

      class MyVC: UIViewController, UIPopoverPresentationControllerDelegate {
      
    2. 在 'prepare for segue' 函数中设置委托和源视图 alpha(在弹出框的路上)

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          let controller = segue.destination as! ISACGlossaryTVC
          controller.popoverPresentationController!.delegate = self
          self.view.alpha = 0.2;
      }
      
    3. 创建委托方法 popoverPresentationControllerDidDismissPopover ,并在弹出窗口被解除时重置源视图 alpha

      func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
          self.view.alpha = 1.0;
      }
      

    【讨论】:

      【解决方案3】:

      我无法通过发布的答案达到预期的效果。但是,这两行代码对我有用:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-20
        • 2011-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-01
        • 1970-01-01
        相关资源
        最近更新 更多