【问题标题】:Disable Dismiss Popover On Background Tap Swift在后台点击 Swift 时禁用关闭弹出框
【发布时间】:2017-07-20 02:56:10
【问题描述】:

我有一个名为TestViewController 的主视图控制器,它有一个按钮,当您点击该按钮时,它会打开一个弹出视图控制器。当您点击背景时,弹出框会被关闭,这是我想要禁用的。我在我的弹出视图控制器中有这段代码,它应该运行但它没有运行。

extension TestViewController: UIPopoverPresentationControllerDelegate {

    func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        print ("TEST") //This does not show up in console

        return false
    }
}

编辑:

这是我用来打开弹出框的代码。

let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopOverViewController

        popover.modalPresentationStyle = .popover

        popover.popoverPresentationController?.sourceView = self.view
        popover.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)

        popover.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)


        popoverPresentationController?.passthroughViews = nil

        popover.dimView2 = self.dimView2

        dimView2.isHidden = false

        self.present(popover, animated: false)

    }

【问题讨论】:

  • 你设置了代理吗?发布您的代码
  • @Bilal 我更新了我的问题。
  • @Bilal 你说得对,我忘了加popover.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate。如果您将其创建为答案,我会为您投票。

标签: swift uipopovercontroller


【解决方案1】:

设置委托。

popover.popoverPresentationController?.delegate = self

【讨论】:

    【解决方案2】:

    popoverPresentationControllerShouldDismissPopover 函数在 iOS 14 中已弃用。

    对于最新版本,您应该使用以下代码

    extension TestViewController: UIPopoverPresentationControllerDelegate {
            func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
                 return false
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-11-10
      • 1970-01-01
      • 2017-01-07
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多