简体   繁体   中英

how to peek and pop view controllers in iOS 13

Peek and pop (UIViewControllerPreviewing) is deprecated in iOS 13. It is to be replaced with the new contextMenuInteraction stuff.

So how do I replace peek and pop directly? In other words, how do I display the target view controller as a preview (peek) and then let the user pop from there to the presentation of the actual target view controller?

Here's what I'm trying, but it isn't the same as the old peek and pop:

override func viewDidLoad() {
    super.viewDidLoad()
    let inter = UIContextMenuInteraction(delegate: self)
    self.view.addInteraction(inter)
}

func contextMenuInteraction(_ inter: UIContextMenuInteraction, 
    configurationForMenuAtLocation loc: CGPoint) -> UIContextMenuConfiguration? {
        let config = UIContextMenuConfiguration(identifier: "preview" as NSString, 
            previewProvider: { SecondViewController() }, actionProvider: nil)
        return config
}

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, 
    willCommitWithAnimator animator: UIContextMenuInteractionCommitAnimating) {
        // animator.preferredCommitStyle = .pop
        if let vc = animator.previewViewController {
            animator.addCompletion {
                self.present(vc, animated: true, completion: nil)
            }
        }
}

In this simple test, the entire background view of the first view controller becomes a place where we can long press to see the preview of the second view controller. What happens is that we do see the preview, and the user can tap it. But then the preview is dismissed and then the second view controller is presented.

That's not peek and pop. Peek and pop means the preview becomes the second view controller. It should grow to become the presentation ("pop").

I tried setting the animator's preferredCommitStyle to .pop (as shown in a commented-out line), but it didn't make any difference.

Has anyone figured this out?

EDIT Apple's own apps don't do this correctly either. In the Photos app, if you long press a photo to get the contextual menu and preview, if you tap the preview, it is dismissed and then the pushed image appears abruptly. Moreover, if you then try to go back (tap the back button), you crash.

The question was asked during an early phase of iOS 13 beta development. I filed a bug report. It turns out that what I was seeing was a bug, and Apple knew about it. In the later phases of iOS 13 beta development, correct .pop behavior was implemented, and in fact is the default . Thus, it now looks and behaves just like peek and pop did in earlier systems.

So, in my question, I said:

That's not peek and pop. Peek and pop means the preview becomes the second view controller. It should grow to become the presentation ("pop").

That is now just what it does. The bug is fixed and the problem is solved (and I've closed my bug report with Apple).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM