Search code examples
iosswiftuinavigationcontrolleruitabbarcontrolleruinavigationbar

Taking a snapshot of a view including the navigation bar and the tab bar


I'm trying to implement a custom navigation controller transition. After the transition is finished the fromVC should be visible behind the toVC, so the effect should look like the toVC appears on top of the fromVC, partly covering it.

So, to achieve the effect I use a snapshot of the fromView and inside the animateTransitioning(using:) method insert it to the containerView as a subview.

 snapshot = fromVC.view.snapshotView(afterScreenUpdates: false)
 containerView.addSubview(self.snapshot!)

However, the problem is that this snapshot doesn't include the navigation bar and the tab bar. I've found some answers that suggest to use UIGraphicsGetImageFromCurrentImageContext() but this returns a UIImage. I need the snapshot to be a UIView to insert it to the containerView as a subview. I could use it and create a UIView manually with a UIImageView and insert it as a subview, but I'm afraid, that's too "heavy" operation for a transition.

If you know how this could be achieved, I would appreciate your help.


Solution

  • This will snapshot and unwrap the root view which includes everything except the status bar:

    guard let snap = UIApplication.shared.keyWindow!.snapshotView(afterScreenUpdates: true) else {
        return  
    }