William Taylor William Taylor - 1 year ago 84
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download