Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(ios) Remove fake status bar with hardcoded height to fix issues in iOS devices with a notch #656

Merged
merged 4 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/ios/CDVInAppBrowserNavigationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ Licensed to the Apache Software Foundation (ASF) under one

#import "CDVInAppBrowserNavigationController.h"

#define STATUSBAR_HEIGHT 20.0

@implementation CDVInAppBrowserNavigationController : UINavigationController

- (void) dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
Expand All @@ -30,16 +28,6 @@ - (void) dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))com
}

- (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];
}

Expand Down
61 changes: 43 additions & 18 deletions src/ios/CDVWKInAppBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Licensed to the Apache Software Foundation (ASF) under one
#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))

Expand Down Expand Up @@ -696,7 +695,7 @@ @implementation CDVWKInAppBrowserViewController

@synthesize currentURL;

BOOL viewRenderedAtLeastOnce = FALSE;
CGFloat lastReducedStatusBarHeight = 0.0;
BOOL isExiting = FALSE;

- (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary *)settings
Expand Down Expand Up @@ -894,7 +893,7 @@ - (void)createViews
[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];
Expand Down Expand Up @@ -1040,7 +1039,6 @@ - (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition

- (void)viewDidLoad
{
viewRenderedAtLeastOnce = FALSE;
[super viewDidLoad];
}

Expand Down Expand Up @@ -1097,14 +1095,6 @@ - (void)goForward:(id)sender

- (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];
Expand All @@ -1116,16 +1106,28 @@ - (void)viewWillAppear:(BOOL)animated
// 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
Expand Down Expand Up @@ -1236,4 +1238,27 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientations
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<UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
[self rePositionViews];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{

}];

[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}


@end //CDVWKInAppBrowserViewController