Skip to content

Commit 2706f34

Browse files
PDLMobileAppsAlessandro Basso
andauthored
(ios) Remove fake status bar with hardcoded height to fix issues in iOS devices with a notch (apache#656)
* (ios) Removed fake statusbar with hardcoded height to fix issues in iOS devices with a notch * (ios) Removed no longer needed bgToolbar * (ios) Fixed issue with rotation in landscape mode and refactored/simplified the code Co-authored-by: Alessandro Basso <[email protected]>
1 parent dcfe692 commit 2706f34

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/ios/CDVInAppBrowserNavigationController.m

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ Licensed to the Apache Software Foundation (ASF) under one
1919

2020
#import "CDVInAppBrowserNavigationController.h"
2121

22-
#define STATUSBAR_HEIGHT 20.0
23-
2422
@implementation CDVInAppBrowserNavigationController : UINavigationController
2523

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

3230
- (void) viewDidLoad {
33-
34-
CGRect statusBarFrame = [self invertFrameIfNeeded:[UIApplication sharedApplication].statusBarFrame];
35-
statusBarFrame.size.height = STATUSBAR_HEIGHT;
36-
// simplified from: http://stackoverflow.com/a/25669695/219684
37-
38-
UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:statusBarFrame];
39-
bgToolbar.barStyle = UIBarStyleDefault;
40-
[bgToolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
41-
[self.view addSubview:bgToolbar];
42-
4331
[super viewDidLoad];
4432
}
4533

src/ios/CDVWKInAppBrowser.m

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Licensed to the Apache Software Foundation (ASF) under one
3535
#define IAB_BRIDGE_NAME @"cordova_iab"
3636

3737
#define TOOLBAR_HEIGHT 44.0
38-
#define STATUSBAR_HEIGHT 20.0
3938
#define LOCATIONBAR_HEIGHT 21.0
4039
#define FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT))
4140

@@ -698,7 +697,7 @@ @implementation CDVWKInAppBrowserViewController
698697

699698
@synthesize currentURL;
700699

701-
BOOL viewRenderedAtLeastOnce = FALSE;
700+
CGFloat lastReducedStatusBarHeight = 0.0;
702701
BOOL isExiting = FALSE;
703702

704703
- (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary *)settings
@@ -896,7 +895,7 @@ - (void)createViews
896895
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
897896
}
898897

899-
self.view.backgroundColor = [UIColor grayColor];
898+
self.view.backgroundColor = [UIColor clearColor];
900899
[self.view addSubview:self.toolbar];
901900
[self.view addSubview:self.addressLabel];
902901
[self.view addSubview:self.spinner];
@@ -1042,7 +1041,6 @@ - (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition
10421041

10431042
- (void)viewDidLoad
10441043
{
1045-
viewRenderedAtLeastOnce = FALSE;
10461044
[super viewDidLoad];
10471045
}
10481046

@@ -1103,14 +1101,6 @@ - (void)goForward:(id)sender
11031101

11041102
- (void)viewWillAppear:(BOOL)animated
11051103
{
1106-
if (IsAtLeastiOSVersion(@"7.0") && !viewRenderedAtLeastOnce) {
1107-
viewRenderedAtLeastOnce = TRUE;
1108-
CGRect viewBounds = [self.webView bounds];
1109-
viewBounds.origin.y = STATUSBAR_HEIGHT;
1110-
viewBounds.size.height = viewBounds.size.height - STATUSBAR_HEIGHT;
1111-
self.webView.frame = viewBounds;
1112-
[[UIApplication sharedApplication] setStatusBarStyle:[self preferredStatusBarStyle]];
1113-
}
11141104
[self rePositionViews];
11151105

11161106
[super viewWillAppear:animated];
@@ -1122,16 +1112,28 @@ - (void)viewWillAppear:(BOOL)animated
11221112
// change that value.
11231113
//
11241114
- (float) getStatusBarOffset {
1125-
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
1126-
float statusBarOffset = IsAtLeastiOSVersion(@"7.0") ? MIN(statusBarFrame.size.width, statusBarFrame.size.height) : 0.0;
1127-
return statusBarOffset;
1115+
return (float) IsAtLeastiOSVersion(@"7.0") ? [[UIApplication sharedApplication] statusBarFrame].size.height : 0.0;
11281116
}
11291117

11301118
- (void) rePositionViews {
1131-
if ([_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
1132-
[self.webView setFrame:CGRectMake(self.webView.frame.origin.x, TOOLBAR_HEIGHT, self.webView.frame.size.width, self.webView.frame.size.height)];
1133-
[self.toolbar setFrame:CGRectMake(self.toolbar.frame.origin.x, [self getStatusBarOffset], self.toolbar.frame.size.width, self.toolbar.frame.size.height)];
1119+
CGRect viewBounds = [self.webView bounds];
1120+
CGFloat statusBarHeight = [self getStatusBarOffset];
1121+
1122+
// orientation portrait or portraitUpsideDown: status bar is on the top and web view is to be aligned to the bottom of the status bar
1123+
// orientation landscapeLeft or landscapeRight: status bar height is 0 in but lets account for it in case things ever change in the future
1124+
viewBounds.origin.y = statusBarHeight;
1125+
1126+
// account for web view height portion that may have been reduced by a previous call to this method
1127+
viewBounds.size.height = viewBounds.size.height - statusBarHeight + lastReducedStatusBarHeight;
1128+
lastReducedStatusBarHeight = statusBarHeight;
1129+
1130+
if ((_browserOptions.toolbar) && ([_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop])) {
1131+
// if we have to display the toolbar on top of the web view, we need to account for its height
1132+
viewBounds.origin.y += TOOLBAR_HEIGHT;
1133+
self.toolbar.frame = CGRectMake(self.toolbar.frame.origin.x, statusBarHeight, self.toolbar.frame.size.width, self.toolbar.frame.size.height);
11341134
}
1135+
1136+
self.webView.frame = viewBounds;
11351137
}
11361138

11371139
// Helper function to convert hex color string to UIColor
@@ -1242,4 +1244,27 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientations
12421244
return 1 << UIInterfaceOrientationPortrait;
12431245
}
12441246

1247+
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
1248+
{
1249+
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
1250+
return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation];
1251+
}
1252+
1253+
return YES;
1254+
}
1255+
1256+
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
1257+
{
1258+
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
1259+
{
1260+
[self rePositionViews];
1261+
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
1262+
{
1263+
1264+
}];
1265+
1266+
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
1267+
}
1268+
1269+
12451270
@end //CDVWKInAppBrowserViewController

0 commit comments

Comments
 (0)