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

CB-9841 - Allow iOS statusbar to animate hide/show #123

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ Hide the statusbar.

StatusBar.hide();

On iOS, you can pass 2 optional parameters, animated and duration, in order to animate hiding the statusbar. By default, the animated parameter is set to false.


Supported Platforms
-------------------
Expand All @@ -284,13 +286,22 @@ Supported Platforms
- Android
- Windows

Quick Example
-------------

StatusBar.hide();
StatusBar.hide(true); // => default duration value is 0.1s
StatusBar.hide(true, 0.3);

StatusBar.show
=================

Shows the statusbar.

StatusBar.show();

On iOS, you can pass 2 optional parameters, animated and duration in seconds, in order to animate showing the statusbar. By default, the animated parameter is set to false.


Supported Platforms
-------------------
Expand All @@ -299,6 +310,13 @@ Supported Platforms
- Android
- Windows

Quick Example
-------------

StatusBar.show();
StatusBar.show(true); // => default duration value is 0.1s
StatusBar.show(true, 0.3);

StatusBar.isVisible
=================

Expand Down
60 changes: 48 additions & 12 deletions src/ios/CDVStatusBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ - (void)pluginInitialize
} else {
self.webView.scrollView.scrollsToTop = NO;
}

// blank scroll view to intercept status bar taps
UIScrollView *fakeScrollView = [[UIScrollView alloc] initWithFrame:UIScreen.mainScreen.bounds];
fakeScrollView.delegate = self;
Expand Down Expand Up @@ -257,17 +257,33 @@ - (void) overlaysWebView:(CDVInvokedUrlCommand*)command
self.statusBarOverlaysWebView = [value boolValue];
}

- (void) refreshStatusBarAppearance
- (void) refreshStatusBarAppearanceWithAnimation:(BOOL)refreshAnimated duration:(NSTimeInterval)duration
{
SEL sel = NSSelectorFromString(@"setNeedsStatusBarAppearanceUpdate");
if ([self.viewController respondsToSelector:sel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self.viewController performSelector:sel withObject:nil];
if (refreshAnimated) {
[UIView animateWithDuration:duration animations:^() {
[self.viewController performSelector:sel withObject:nil];
}completion:^(BOOL finished){}];
} else {
[self.viewController performSelector:sel withObject:nil];
}
#pragma clang diagnostic pop
}
}

- (void) refreshStatusBarAppearanceWithAnimation:(BOOL)refreshAnimated
{
[self refreshStatusBarAppearanceWithAnimation:refreshAnimated duration:0.1];
}

- (void) refreshStatusBarAppearance
{
[self refreshStatusBarAppearanceWithAnimation:NO];
}

- (void) setStyleForStatusBar:(UIStatusBarStyle)style
{
if (_uiviewControllerBasedStatusBarAppearance) {
Expand Down Expand Up @@ -359,16 +375,16 @@ - (void) backgroundColorByHexString:(CDVInvokedUrlCommand*)command
[self _backgroundColorByHexString:value];
}

- (void) hideStatusBar
- (void) hideStatusBarWithAnimation:(BOOL)animated duration:(NSTimeInterval)duration
{
if (_uiviewControllerBasedStatusBarAppearance) {
CDVViewController* vc = (CDVViewController*)self.viewController;
vc.sb_hideStatusBar = [NSNumber numberWithBool:YES];
[self refreshStatusBarAppearance];
[self refreshStatusBarAppearanceWithAnimation:animated duration:duration];

} else {
UIApplication* app = [UIApplication sharedApplication];
[app setStatusBarHidden:YES];
[app setStatusBarHidden:YES withAnimation:animated];
}
}

Expand All @@ -380,7 +396,17 @@ - (void) hide:(CDVInvokedUrlCommand*)command
if (!app.isStatusBarHidden)
{

[self hideStatusBar];
id animated = [command argumentAtIndex:0];
if (!([animated isKindOfClass:[NSNumber class]])) {
animated = [NSNumber numberWithBool:NO];
}

id duration = [command argumentAtIndex:1];
if (!([duration isKindOfClass:[NSNumber class]])) {
duration = [NSNumber numberWithDouble:0.1];
}

[self hideStatusBarWithAnimation:[animated boolValue] duration:[duration doubleValue]];

[_statusBarBackgroundView removeFromSuperview];

Expand All @@ -390,16 +416,16 @@ - (void) hide:(CDVInvokedUrlCommand*)command
}
}

- (void) showStatusBar
- (void) showStatusBarWithAnimation:(BOOL)animated duration:(NSTimeInterval)duration
{
if (_uiviewControllerBasedStatusBarAppearance) {
CDVViewController* vc = (CDVViewController*)self.viewController;
vc.sb_hideStatusBar = [NSNumber numberWithBool:NO];
[self refreshStatusBarAppearance];
[self refreshStatusBarAppearanceWithAnimation:animated duration:duration];

} else {
UIApplication* app = [UIApplication sharedApplication];
[app setStatusBarHidden:NO];
[app setStatusBarHidden:NO withAnimation:animated];
}
}

Expand All @@ -410,7 +436,17 @@ - (void) show:(CDVInvokedUrlCommand*)command

if (app.isStatusBarHidden)
{
[self showStatusBar];
id animated = [command argumentAtIndex:0];
if (!([animated isKindOfClass:[NSNumber class]])) {
animated = [NSNumber numberWithBool:NO];
}

id duration = [command argumentAtIndex:1];
if (!([duration isKindOfClass:[NSNumber class]])) {
duration = [NSNumber numberWithDouble:0.1];
}

[self showStatusBarWithAnimation:[animated boolValue] duration:[duration doubleValue]];
[self resizeWebView];

if (!self.statusBarOverlaysWebView) {
Expand Down Expand Up @@ -471,7 +507,7 @@ -(void)resizeWebView
}
frame.size.height -= frame.origin.y;
self.webView.frame = frame;

}

- (void) dealloc
Expand Down
8 changes: 4 additions & 4 deletions www/statusbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ var StatusBar = {
exec(null, null, "StatusBar", "backgroundColorByHexString", [hexString]);
},

hide: function () {
exec(null, null, "StatusBar", "hide", []);
hide: function (animated, duration) {
exec(null, null, "StatusBar", "hide", [animated, duration]);
StatusBar.isVisible = false;
},

show: function () {
exec(null, null, "StatusBar", "show", []);
show: function (animated, duration) {
exec(null, null, "StatusBar", "show", [animated, duration]);
StatusBar.isVisible = true;
}

Expand Down