0

看起来 - 至少在默认情况下,虽然您可以在 plus iPhone 上与弹出框的内容交互并通过点击背景将其关闭,但在普通的非 plus 手机上,行为是相反的。

有谁知道如何纠正和/或配置此问题,以便您可以与普通 iPhone 上的弹出框交互?

我有一个在这里演示问题的示例:
https://github.com/chrisco314/iPhone-Popover-Test

但相关代码是:

class PresentingViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configure()
    }

    func configure() {
        view.addSubview(button)
        view.backgroundColor = .white
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
    }

    lazy var button: UIButton = {
        let button = UIButton()
        button.layer.cornerRadius = 10
        button.contentEdgeInsets = .init(top: 8, left: 8, bottom: 8, right: 8)
        button.backgroundColor = .blue
        button.setTitle("Show popover", for: .normal)
        button.addTarget(self, action: #selector(didTap(sender:)), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @objc func didTap(sender: UIButton) {
        let presented = PresentedViewController()
        presented.modalPresentationStyle = .popover

        let popover = presented.popoverPresentationController!
        popover.delegate = self
        popover.sourceRect = sender.bounds
        popover.sourceView = sender
        popover.permittedArrowDirections = .up
        popover.backgroundColor = popover.presentedViewController.view.backgroundColor

        self.present(presented, animated: true, completion: {})
    }
}

extension PresentingViewController: UIPopoverPresentationControllerDelegate {

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


class PresentedViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configure()
    }

    func configure() {
        view.backgroundColor = .green
        view.addSubview(text)
        text.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
        view.rightAnchor.constraint(equalTo: text.rightAnchor, constant: 20).isActive = true
        text.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
        view.bottomAnchor.constraint(greaterThanOrEqualTo: text.bottomAnchor, constant: 50).isActive = true
    }

    lazy var text: UITextField = {
        let view = UITextField()
        view.text = "Placeholder"
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .blue
        return view
    }()
}

谢谢!

4

1 回答 1

0

我现在在想,这可能是一个模拟器问题。我在我的主要项目中看到了这个,一个单独的样本,然后写了一个新的更清洁的样本供公众使用。然后我在不同版本的模拟器之间来回切换,它又开始工作了。

诡异的。

于 2018-05-10T16:12:24.760 回答