diff --git a/src/ios/CDVInAppBrowserNavigationController.m b/src/ios/CDVInAppBrowserNavigationController.m index ff66487..3cc9043 100644 --- a/src/ios/CDVInAppBrowserNavigationController.m +++ b/src/ios/CDVInAppBrowserNavigationController.m @@ -19,8 +19,6 @@ #import "CDVInAppBrowserNavigationController.h" -#define STATUSBAR_HEIGHT 20.0 - @implementation CDVInAppBrowserNavigationController : UINavigationController - (void) dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion { @@ -30,16 +28,6 @@ } - (void) viewDidLoad { - - CGRect statusBarFrame = [self invertFrameIfNeeded:[UIApplication sharedApplication].statusBarFrame]; - statusBarFrame.size.height = STATUSBAR_HEIGHT; - // simplified from: http://stackoverflow.com/a/25669695/219684 - - UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:statusBarFrame]; - bgToolbar.barStyle = UIBarStyleDefault; - [bgToolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; - [self.view addSubview:bgToolbar]; - [super viewDidLoad]; } diff --git a/src/ios/CDVWKInAppBrowser.m b/src/ios/CDVWKInAppBrowser.m index e74734c..3a2ecf8 100644 --- a/src/ios/CDVWKInAppBrowser.m +++ b/src/ios/CDVWKInAppBrowser.m @@ -35,7 +35,6 @@ #define IAB_BRIDGE_NAME @"cordova_iab" #define TOOLBAR_HEIGHT 44.0 -#define STATUSBAR_HEIGHT 20.0 #define LOCATIONBAR_HEIGHT 21.0 #define FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT)) @@ -698,7 +697,7 @@ static CDVWKInAppBrowser* instance = nil; @synthesize currentURL; -BOOL viewRenderedAtLeastOnce = FALSE; +CGFloat lastReducedStatusBarHeight = 0.0; BOOL isExiting = FALSE; - (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary *)settings @@ -896,7 +895,7 @@ BOOL isExiting = FALSE; [self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]]; } - self.view.backgroundColor = [UIColor grayColor]; + self.view.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.toolbar]; [self.view addSubview:self.addressLabel]; [self.view addSubview:self.spinner]; @@ -1042,7 +1041,6 @@ BOOL isExiting = FALSE; - (void)viewDidLoad { - viewRenderedAtLeastOnce = FALSE; [super viewDidLoad]; } @@ -1103,14 +1101,6 @@ BOOL isExiting = FALSE; - (void)viewWillAppear:(BOOL)animated { - if (IsAtLeastiOSVersion(@"7.0") && !viewRenderedAtLeastOnce) { - viewRenderedAtLeastOnce = TRUE; - CGRect viewBounds = [self.webView bounds]; - viewBounds.origin.y = STATUSBAR_HEIGHT; - viewBounds.size.height = viewBounds.size.height - STATUSBAR_HEIGHT; - self.webView.frame = viewBounds; - [[UIApplication sharedApplication] setStatusBarStyle:[self preferredStatusBarStyle]]; - } [self rePositionViews]; [super viewWillAppear:animated]; @@ -1122,16 +1112,28 @@ BOOL isExiting = FALSE; // change that value. // - (float) getStatusBarOffset { - CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; - float statusBarOffset = IsAtLeastiOSVersion(@"7.0") ? MIN(statusBarFrame.size.width, statusBarFrame.size.height) : 0.0; - return statusBarOffset; + return (float) IsAtLeastiOSVersion(@"7.0") ? [[UIApplication sharedApplication] statusBarFrame].size.height : 0.0; } - (void) rePositionViews { - if ([_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop]) { - [self.webView setFrame:CGRectMake(self.webView.frame.origin.x, TOOLBAR_HEIGHT, self.webView.frame.size.width, self.webView.frame.size.height)]; - [self.toolbar setFrame:CGRectMake(self.toolbar.frame.origin.x, [self getStatusBarOffset], self.toolbar.frame.size.width, self.toolbar.frame.size.height)]; + CGRect viewBounds = [self.webView bounds]; + CGFloat statusBarHeight = [self getStatusBarOffset]; + + // orientation portrait or portraitUpsideDown: status bar is on the top and web view is to be aligned to the bottom of the status bar + // orientation landscapeLeft or landscapeRight: status bar height is 0 in but lets account for it in case things ever change in the future + viewBounds.origin.y = statusBarHeight; + + // account for web view height portion that may have been reduced by a previous call to this method + viewBounds.size.height = viewBounds.size.height - statusBarHeight + lastReducedStatusBarHeight; + lastReducedStatusBarHeight = statusBarHeight; + + if ((_browserOptions.toolbar) && ([_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop])) { + // if we have to display the toolbar on top of the web view, we need to account for its height + viewBounds.origin.y += TOOLBAR_HEIGHT; + self.toolbar.frame = CGRectMake(self.toolbar.frame.origin.x, statusBarHeight, self.toolbar.frame.size.width, self.toolbar.frame.size.height); } + + self.webView.frame = viewBounds; } // Helper function to convert hex color string to UIColor @@ -1242,4 +1244,27 @@ BOOL isExiting = FALSE; return 1 << UIInterfaceOrientationPortrait; } +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation +{ + if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) { + return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation]; + } + + return YES; +} + +- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator +{ + [coordinator animateAlongsideTransition:^(id context) + { + [self rePositionViews]; + } completion:^(id context) + { + + }]; + + [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; +} + + @end //CDVWKInAppBrowserViewController