Interacting with contents of UIPopoverPresentationController on regular sized iPhones?

本小妞迷上赌 提交于 2019-12-13 03:50:00

问题


It appears - at least by default, that while you can interact with the contents of a popover on a plus iPhone and dismiss it by tapping on the background, on a regular, non plus phone, the behavior is the opposite.

Does anyone know how to correct and/or configure this so that you can interact with a popover on an regular iPhone?

I have a sample that demonstrates the problem here:
https://github.com/chrisco314/iPhone-Popover-Test,

but the relevant code is:

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
    }()
}

Thanks!


回答1:


I am thinking now that this might have been a simulator problem. I had seen this on my main project, a separate sample, and then wrote a new cleaner sample for public consumption. I then switched back and forth between different versions of the simulator and it started working again.

Weird.



来源:https://stackoverflow.com/questions/50262423/interacting-with-contents-of-uipopoverpresentationcontroller-on-regular-sized-ip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!