diff --git a/src/ios/CDVStatusBar.m b/src/ios/CDVStatusBar.m index 8578f29..d102018 100644 --- a/src/ios/CDVStatusBar.m +++ b/src/ios/CDVStatusBar.m @@ -90,6 +90,16 @@ static const void *kStatusBarStyle = &kStatusBarStyle; } } +-(void)statusBarDidChangeFrame:(NSNotification*)notification +{ + //add a small delay for iOS 7 + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + + [self resizeWebView]; + + }); +} + - (void)pluginInitialize { BOOL isiOS7 = (IsAtLeastiOSVersion(@"7.0")); @@ -101,6 +111,8 @@ static const void *kStatusBarStyle = &kStatusBarStyle; // observe the statusBarHidden property [[UIApplication sharedApplication] addObserver:self forKeyPath:@"statusBarHidden" options:NSKeyValueObservingOptionNew context:NULL]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarDidChangeFrame:) name: UIApplicationDidChangeStatusBarFrameNotification object:nil]; + _statusBarOverlaysWebView = YES; // default [self initializeStatusBarBackgroundView]; @@ -109,11 +121,6 @@ static const void *kStatusBarStyle = &kStatusBarStyle; NSString* setting; - setting = @"StatusBarOverlaysWebView"; - if ([self settingForKey:setting]) { - self.statusBarOverlaysWebView = [(NSNumber*)[self settingForKey:setting] boolValue]; - } - setting = @"StatusBarBackgroundColor"; if ([self settingForKey:setting]) { [self _backgroundColorByHexString:[self settingForKey:setting]]; @@ -158,11 +165,17 @@ static const void *kStatusBarStyle = &kStatusBarStyle; [self.commandDelegate sendPluginResult:result callbackId:_eventsCallbackId]; } - - (void) _ready:(CDVInvokedUrlCommand*)command { _eventsCallbackId = command.callbackId; [self updateIsVisible:![UIApplication sharedApplication].statusBarHidden]; + NSString* setting = @"StatusBarOverlaysWebView"; + if ([self settingForKey:setting]) { + self.statusBarOverlaysWebView = [(NSNumber*)[self settingForKey:setting] boolValue]; + if (self.statusBarOverlaysWebView) { + [self resizeWebView]; + } + } } - (void) initializeStatusBarBackgroundView @@ -195,34 +208,22 @@ static const void *kStatusBarStyle = &kStatusBarStyle; if (!IsAtLeastiOSVersion(@"7.0") || statusBarOverlaysWebView == _statusBarOverlaysWebView) { return; } + + _statusBarOverlaysWebView = statusBarOverlaysWebView; - CGRect bounds = [[UIScreen mainScreen] bounds]; + [self resizeWebView]; if (statusBarOverlaysWebView) { [_statusBarBackgroundView removeFromSuperview]; - if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) { - self.webView.frame = CGRectMake(0, 0, bounds.size.height, bounds.size.width); - } else { - self.webView.frame = bounds; - } } else { - CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; - statusBarFrame = [self invertFrameIfNeeded:statusBarFrame orientation:self.viewController.interfaceOrientation]; - [self initializeStatusBarBackgroundView]; - - CGRect frame = self.webView.frame; - frame.origin.y = statusBarFrame.size.height; - frame.size.height -= statusBarFrame.size.height; - - self.webView.frame = frame; [self.webView.superview addSubview:_statusBarBackgroundView]; + } - _statusBarOverlaysWebView = statusBarOverlaysWebView; } - (BOOL) statusBarOverlaysWebView @@ -366,24 +367,14 @@ static const void *kStatusBarStyle = &kStatusBarStyle; if (!app.isStatusBarHidden) { - CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; - + [self hideStatusBar]; if (IsAtLeastiOSVersion(@"7.0")) { [_statusBarBackgroundView removeFromSuperview]; } - if (!_statusBarOverlaysWebView) { - - CGRect frame = self.webView.frame; - frame.origin.y = 0; - if (!self.statusBarOverlaysWebView) { - frame.size.height += MIN(statusBarFrame.size.height, statusBarFrame.size.width); - } - - self.webView.frame = frame; - } + [self resizeWebView]; _statusBarBackgroundView.hidden = YES; } @@ -413,27 +404,22 @@ static const void *kStatusBarStyle = &kStatusBarStyle; [self showStatusBar]; if (isIOS7) { - CGRect frame = self.webView.frame; - self.viewController.view.frame = [[UIScreen mainScreen] bounds]; - CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; - statusBarFrame = [self invertFrameIfNeeded:statusBarFrame orientation:self.viewController.interfaceOrientation]; + [self resizeWebView]; if (!self.statusBarOverlaysWebView) { // there is a possibility that when the statusbar was hidden, it was in a different orientation // from the current one. Therefore we need to expand the statusBarBackgroundView as well to the // statusBar's current size + CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; + statusBarFrame = [self invertFrameIfNeeded:statusBarFrame orientation:self.viewController.interfaceOrientation]; CGRect sbBgFrame = _statusBarBackgroundView.frame; - frame.origin.y = statusBarFrame.size.height; - frame.size.height -= statusBarFrame.size.height; sbBgFrame.size = statusBarFrame.size; - _statusBarBackgroundView.frame = sbBgFrame; [self.webView.superview addSubview:_statusBarBackgroundView]; - } - self.webView.frame = frame; + } } else { @@ -445,9 +431,35 @@ static const void *kStatusBarStyle = &kStatusBarStyle; } } +-(void)resizeWebView { + + CGRect bounds = [[UIScreen mainScreen] bounds]; + + bounds = [self invertFrameIfNeeded:bounds orientation:self.viewController.interfaceOrientation]; + + if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) { + self.viewController.view.frame = bounds; + } + + self.webView.frame = bounds; + + if (!self.statusBarOverlaysWebView) { + + CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; + statusBarFrame = [self invertFrameIfNeeded:statusBarFrame orientation:self.viewController.interfaceOrientation]; + + CGRect frame = self.webView.frame; + frame.origin.y = statusBarFrame.size.height; + frame.size.height -= statusBarFrame.size.height; + self.webView.frame = frame; + } + +} + - (void) dealloc { [[UIApplication sharedApplication] removeObserver:self forKeyPath:@"statusBarHidden"]; + [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:UIApplicationDidChangeStatusBarFrameNotification]; }