Skip to content

Commit

Permalink
Fix bugs with status bar and keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Friend-LGA committed Apr 12, 2017
1 parent 3d9ea37 commit d99e2ec
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 22 deletions.
22 changes: 16 additions & 6 deletions LGAlertView/LGAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1102,13 +1102,25 @@ - (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 {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
70 changes: 55 additions & 15 deletions LGAlertView/LGAlertViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#import "LGAlertViewController.h"
#import "LGAlertView.h"
#import "UIWindow+LGAlertView.h"
#import "LGAlertViewHelper.h"

@interface LGAlertViewController ()

Expand All @@ -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<UIViewControllerTransitionCoordinator>)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<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
UIViewController *viewController = LGAlertViewHelper.appWindow.currentViewController;

if (viewController) {
return viewController.supportedInterfaceOrientations;
}

[coordinator
animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> 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
2 changes: 2 additions & 0 deletions LGAlertView/LGAlertViewHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ extern CGFloat const LGAlertViewButtonImageOffsetFromTitle;
+ (UIWindow *)appWindow;
+ (UIWindow *)keyWindow;

+ (BOOL)isViewControllerBasedStatusBarAppearance;

@end
19 changes: 18 additions & 1 deletion LGAlertView/LGAlertViewHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d99e2ec

Please sign in to comment.