如何在swift中隐藏UIPopoverPresentationController的模糊?

时间:2018-02-14 15:46:33

标签: ios swift uipopovercontroller

我为UIButton实现了popover:

    @objc func showList(sender: AnyObject?) {

        guard let buttonView = sender?.value(forKey: "view") as? UIButton else { return }
        guard let popVC = R.storyboard.first.VC() else { return }
        popVC.modalPresentationStyle = .popover
        let popOverVC = popVC.popoverPresentationController
        popOverVC?.delegate = self
        popOverVC?.sourceView = buttonView
        popOverVC?.sourceRect = CGRect(x: 0, y: 190, width: 0, height: 0)
        popOverVC?.permittedArrowDirections = .init(rawValue: 0)

        popVC.preferredContentSize = CGSize(width: 150, height: 250)

        showedViewController.present(popVC, animated: true, completion: nil)
    }


extension VCInteractor: UIPopoverPresentationControllerDelegate {

    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
}

它运行良好,但我需要一个修复 - 我想为我的popover隐藏UIVisualEffectBackdropView(就像我的UITableViewController下的模糊一样)。所以,我找不到任何关于如何禁用(隐藏)这种模糊的信息,你有什么想法吗?

2 个答案:

答案 0 :(得分:5)

我已使用自定义类修复了我的问题

class PopoverBackgroundView: UIPopoverBackgroundView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.shadowColor = UIColor.clear.cgColor

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override static func contentViewInsets() -> UIEdgeInsets {
        return UIEdgeInsets.zero
    }

    override static func arrowHeight() -> CGFloat {
        return 0
    }

    override var arrowDirection: UIPopoverArrowDirection {
        get { return UIPopoverArrowDirection.down }
        set {
            setNeedsLayout()
        }
    }

    override var arrowOffset: CGFloat {
        get { return 0 }
        set {
            setNeedsLayout()
        }
    }
}

我已经添加了它

popOverVC?.popoverBackgroundViewClass = PopoverBackgroundView.self

这就是全部

答案 1 :(得分:0)

在这里,我有一个非常简单的解决方案。

    let appearance = UIVisualEffectView.appearance(whenContainedInInstancesOf: [NSClassFromString("_UIPopoverStandardChromeView")! as! UIAppearanceContainer.Type])
    appearance.effect = UIVisualEffect()

将代码添加到AppDelegate.swift文件或应用初始化的任何位置。