William Taylor William Taylor - 1 month ago 12
Swift Question

Take Screenshot of ViewController that is not currently open

I am writing an app in Swift. In my app, I have a ViewController. There is a button on it, as well as an image view. When I press that button, I need to take a screenshot of another ViewController, have all the constraints that I defined in Interface Builder for the second ViewController work, and the code in view did load run. I then will put the ScreenShot in the image view. I found this thread here, but it does not appear to use the constraints. I am not sure why.

Thank you for any help in advance!

Answer

UIView now has a snapshot method for making stand in views like this. If you need your view controller to be rendered with correct contraints for the current device, add its view to the back of your view heirarchy (ie insertSubview(viewController.view, at: 0)), take your snapshot, then remove view and release the view controller.