snapshotView(afterScreenUpdates: true) returns an empty view

依然范特西╮ 提交于 2021-02-07 07:46:10

问题


I want to implement a custom push-transition for a navigation controller.

I first implemented the UINavigationControllerDelegate where I return an Object of UIViewControllerAnimatedTransitioning.

In this class, I want to take a snapshot of the destination view in order to animate it. However, for the push-segue, this doesn't work - the snapshot ist empty. [when I pop back, I set afterScreenUpdates = false and everything works]

    guard let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) else {
            return
    }

    guard let snapshot = toVC.view.snapshotView(afterScreenUpdates: true) else {
        return }

   // Problem:
   // snapshot is an empty view!

回答1:


Supplying true for -snapshotViewAfterScreenUpdates: means it needs a trip back to the runloop to actually draw the image. If you supply NO, it will try immediately, but if your view is off screen or otherwise hasn't yet drawn to the screen, the snapshot will be empty.




回答2:


I find that it is necessary to introduce a short delay, say about a tenth of a second, after the push transition and before taking the screenshot, in order to avoid the blank screenshot. Probably this lets the interface settle down in some way.



来源:https://stackoverflow.com/questions/47024439/snapshotviewafterscreenupdates-true-returns-an-empty-view

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