Skip to content

Instantly share code, notes, and snippets.

@SpacyRicochet
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpacyRicochet/73275226302dd87f4f53 to your computer and use it in GitHub Desktop.
Save SpacyRicochet/73275226302dd87f4f53 to your computer and use it in GitHub Desktop.
Hide screen when iOS app resigns active
@interface KIDAppDelegate ()
@property (nonatomic, strong) UIViewController *hideInformationViewController;
@end
@implementation KIDAppDelegate
{
- (void)applicationWillResignActive:(UIApplication *)application
{
[self hideScreen:YES];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self hideScreen:NO];
}
- (void)hideScreen:(BOOL)hide
{
// This causes a crash on iOS 7, when the app is currently transitioning to a modal view controller.
// On iOS 8, this doesn't crash the app, but it's unreliable whether it actually hides the transitioning view controller.
// Doesn't work in Simulator when you put the app directly in the background. If you go through app switcher it works fine. Works fine on device.
if (hide) {
UIViewController *topmostViewController = self.window.rootViewController;
while (topmostViewController.presentedViewController) {
topmostViewController = topmostViewController.presentedViewController;
if ([topmostViewController isKindOfClass:[UINavigationController class]] && !topmostViewController.presentedViewController) {
topmostViewController = [(UINavigationController *)topmostViewController visibleViewController];
}
}
self.hideInformationViewController = [UIViewController new];
UIImage *image = [UIImage imageNamed:@"LaunchImage-700-568h"];
UIImageView *hideImageView = [[UIImageView alloc] initWithImage:image];
hideImageView.contentMode = UIViewContentModeTop;
hideImageView.frame = self.window.bounds;
self.hideInformationViewController.view = hideImageView;
[topmostViewController presentViewController:self.hideInformationViewController animated:NO completion:nil];
}
else {
if (self.hideInformationViewController) {
[self.hideInformationViewController dismissViewControllerAnimated:NO completion:nil];
self.hideInformationViewController = nil;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment