diff --git a/LGAlertView/LGAlertView.m b/LGAlertView/LGAlertView.m index 150656a..9e803ea 100644 --- a/LGAlertView/LGAlertView.m +++ b/LGAlertView/LGAlertView.m @@ -1102,6 +1102,8 @@ - (void)addObservers { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardVisibleChanged:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChanged:) name:UIKeyboardWillChangeFrameNotification object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowVisibleChanged:) name:UIWindowDidBecomeVisibleNotification object:nil]; } - (void)removeObservers { @@ -1109,6 +1111,16 @@ - (void)removeObservers { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil]; + + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil]; +} + +#pragma mark - Window notifications + +- (void)windowVisibleChanged:(NSNotification *)notification { + if (notification.object == self.window) { + [self.viewController.view setNeedsLayout]; + } } #pragma mark - Keyboard notifications @@ -1607,6 +1619,8 @@ - (void)showAnimated:(BOOL)animated hidden:(BOOL)hidden completionHandler:(LGAle UIWindow *keyWindow = LGAlertViewHelper.keyWindow; + [keyWindow endEditing:YES]; + if (!hidden && keyWindow != LGAlertViewHelper.appWindow) { keyWindow.hidden = YES; } @@ -2813,7 +2827,7 @@ - (void)layoutValidateWithSize:(CGSize)size { scrollViewFrame.origin.y += LGAlertViewHelper.statusBarHeight / 2.0; } - if (!self.isVisible) { + if (!self.isShowing) { scrollViewTransform = CGAffineTransformMakeScale(1.2, 1.2); scrollViewAlpha = 0.0; @@ -2859,7 +2873,7 @@ - (void)layoutValidateWithSize:(CGSize)size { self.cancelButtonCenterHidden = CGPointMake(CGRectGetMinX(cancelButtonFrame) + (CGRectGetWidth(cancelButtonFrame) / 2.0), CGRectGetMinY(cancelButtonFrame) + (CGRectGetHeight(cancelButtonFrame) / 2.0) + commonHeight); - if (!self.isVisible) { + if (!self.isShowing) { scrollViewFrame.origin.y += commonHeight; if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) { @@ -3167,10 +3181,6 @@ - (BOOL)isValid { return [self isAlertViewValid:self]; } -- (BOOL)isVisible { - return self.isShowing && self.window.isKeyWindow && !self.window.isHidden; -} - - (CGFloat)innerMarginHeight { return self.style == LGAlertViewStyleAlert ? 16.0 : 12.0; } diff --git a/LGAlertView/LGAlertViewController.m b/LGAlertView/LGAlertViewController.m index f74c073..23bbf2b 100644 --- a/LGAlertView/LGAlertViewController.m +++ b/LGAlertView/LGAlertViewController.m @@ -30,6 +30,7 @@ #import "LGAlertViewController.h" #import "LGAlertView.h" #import "UIWindow+LGAlertView.h" +#import "LGAlertViewHelper.h" @interface LGAlertViewController () @@ -50,34 +51,73 @@ - (nonnull instancetype)initWithAlertView:(nonnull LGAlertView *)alertView view: return self; } -- (BOOL)shouldAutorotate { - UIWindow *window = [UIApplication sharedApplication].delegate.window; +- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { + [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; - return window.currentViewController.shouldAutorotate; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [UIView animateWithDuration:coordinator.transitionDuration animations:^{ + [self setNeedsStatusBarAppearanceUpdate]; + [self.alertView layoutValidateWithSize:size]; + }]; + }); } -- (UIInterfaceOrientationMask)supportedInterfaceOrientations { - UIWindow *window = [UIApplication sharedApplication].delegate.window; +#pragma mark - + +- (BOOL)shouldAutorotate { + UIViewController *viewController = LGAlertViewHelper.appWindow.currentViewController; - return window.currentViewController.supportedInterfaceOrientations; + if (viewController) { + return viewController.shouldAutorotate; + } + + return super.shouldAutorotate; } -- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { - [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; +- (UIInterfaceOrientationMask)supportedInterfaceOrientations { + UIViewController *viewController = LGAlertViewHelper.appWindow.currentViewController; + + if (viewController) { + return viewController.supportedInterfaceOrientations; + } - [coordinator - animateAlongsideTransition:^(id context) { - [self.alertView layoutValidateWithSize:size]; - } - completion:nil]; + return super.supportedInterfaceOrientations; } - (UIStatusBarStyle)preferredStatusBarStyle { - return [UIApplication sharedApplication].statusBarStyle; + if (LGAlertViewHelper.isViewControllerBasedStatusBarAppearance) { + UIViewController *viewController = LGAlertViewHelper.appWindow.currentViewController; + + if (viewController) { + return viewController.preferredStatusBarStyle; + } + } + + return super.preferredStatusBarStyle; } - (BOOL)prefersStatusBarHidden { - return [UIApplication sharedApplication].statusBarHidden; + if (LGAlertViewHelper.isViewControllerBasedStatusBarAppearance) { + UIViewController *viewController = LGAlertViewHelper.appWindow.currentViewController; + + if (viewController) { + return viewController.prefersStatusBarHidden; + } + } + + return super.prefersStatusBarHidden; +} + +- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { + if (LGAlertViewHelper.isViewControllerBasedStatusBarAppearance) { + UIViewController *viewController = LGAlertViewHelper.appWindow.currentViewController; + + if (viewController) { + return viewController.preferredStatusBarUpdateAnimation; + } + } + + return super.preferredStatusBarUpdateAnimation; } @end diff --git a/LGAlertView/LGAlertViewHelper.h b/LGAlertView/LGAlertViewHelper.h index 07c6107..54154e6 100644 --- a/LGAlertView/LGAlertViewHelper.h +++ b/LGAlertView/LGAlertViewHelper.h @@ -66,4 +66,6 @@ extern CGFloat const LGAlertViewButtonImageOffsetFromTitle; + (UIWindow *)appWindow; + (UIWindow *)keyWindow; ++ (BOOL)isViewControllerBasedStatusBarAppearance; + @end diff --git a/LGAlertView/LGAlertViewHelper.m b/LGAlertView/LGAlertViewHelper.m index 4d6518d..09ac071 100644 --- a/LGAlertView/LGAlertViewHelper.m +++ b/LGAlertView/LGAlertViewHelper.m @@ -124,11 +124,28 @@ + (CGFloat)systemVersion { } + (UIWindow *)appWindow { - return [UIApplication sharedApplication].delegate.window; + return [UIApplication sharedApplication].windows[0]; } + (UIWindow *)keyWindow { return [UIApplication sharedApplication].keyWindow; } ++ (BOOL)isViewControllerBasedStatusBarAppearance { + static BOOL isViewControllerBasedStatusBarAppearance; + static dispatch_once_t onceToken; + + dispatch_once(&onceToken, ^{ + if (UIDevice.currentDevice.systemVersion.floatValue >= 9.0) { + isViewControllerBasedStatusBarAppearance = YES; + } + else { + NSNumber *viewControllerBasedStatusBarAppearance = [NSBundle.mainBundle objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"]; + isViewControllerBasedStatusBarAppearance = (viewControllerBasedStatusBarAppearance == nil ? YES : viewControllerBasedStatusBarAppearance.boolValue); + } + }); + + return isViewControllerBasedStatusBarAppearance; +} + @end