CB-6783 - added StatusBarStyle config preference, updated docs (closes #9)

Signed-off-by: Shazron Abdullah <shazron@apache.org>
This commit is contained in:
pelish8 2014-05-15 14:05:14 +02:00 committed by Shazron Abdullah
parent 4cfee575a1
commit 55575f5409
3 changed files with 75 additions and 67 deletions

View File

@ -32,11 +32,15 @@ Preferences
- __StatusBarOverlaysWebView__ (boolean, defaults to true). On iOS 7, make the statusbar overlay or not overlay the WebView at startup. - __StatusBarOverlaysWebView__ (boolean, defaults to true). On iOS 7, make the statusbar overlay or not overlay the WebView at startup.
<preference name="StatusBarOverlaysWebView" value="true" /> <preference name="StatusBarOverlaysWebView" value="true" />
- __StatusBarBackgroundColor__ (color hex string, defaults to #000000). On iOS 7, set the background color of the statusbar by a hex string (#RRGGBB) at startup. - __StatusBarBackgroundColor__ (color hex string, defaults to #000000). On iOS 7, set the background color of the statusbar by a hex string (#RRGGBB) at startup.
<preference name="StatusBarBackgroundColor" value="#000000" /> <preference name="StatusBarBackgroundColor" value="#000000" />
- __StatusBarStyle__ (status bar style, defaults to lightcontent). On iOS 7, set the status bar style. Available options default, lightcontent, blacktranslucent, blackopaque.
<preference name="StatusBarStyle" value="lightcontent" />
Hiding at startup Hiding at startup
----------- -----------
@ -50,7 +54,7 @@ Add/edit these two attributes if not present. Set **"Status bar is initially hid
<key>UIViewControllerBasedStatusBarAppearance</key> <key>UIViewControllerBasedStatusBarAppearance</key>
<false/> <false/>
Methods Methods
------- -------
@ -114,7 +118,7 @@ Supported Platforms
------------------- -------------------
- iOS - iOS
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8
StatusBar.styleLightContent StatusBar.styleLightContent
@ -129,7 +133,7 @@ Supported Platforms
------------------- -------------------
- iOS - iOS
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8
StatusBar.styleBlackTranslucent StatusBar.styleBlackTranslucent
@ -144,7 +148,7 @@ Supported Platforms
------------------- -------------------
- iOS - iOS
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8
StatusBar.styleBlackOpaque StatusBar.styleBlackOpaque
@ -159,7 +163,7 @@ Supported Platforms
------------------- -------------------
- iOS - iOS
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8
@ -179,7 +183,7 @@ Supported Platforms
------------------- -------------------
- iOS - iOS
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8
StatusBar.backgroundColorByHexString StatusBar.backgroundColorByHexString
@ -202,7 +206,7 @@ Supported Platforms
------------------- -------------------
- iOS - iOS
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8
StatusBar.hide StatusBar.hide
@ -218,7 +222,7 @@ Supported Platforms
- iOS - iOS
- Android - Android
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8
StatusBar.show StatusBar.show
@ -234,7 +238,7 @@ Supported Platforms
- iOS - iOS
- Android - Android
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8
@ -253,8 +257,7 @@ Supported Platforms
- iOS - iOS
- Android - Android
- Windows Phone 7 - Windows Phone 7
- Windows Phone 8 - Windows Phone 8

View File

@ -35,7 +35,7 @@
<js-module src="www/statusbar.js" name="statusbar"> <js-module src="www/statusbar.js" name="statusbar">
<clobbers target="window.StatusBar" /> <clobbers target="window.StatusBar" />
</js-module> </js-module>
<platform name="android"> <platform name="android">
<source-file src="src/android/StatusBar.java" target-dir="src/org/apache/cordova/statusbar" /> <source-file src="src/android/StatusBar.java" target-dir="src/org/apache/cordova/statusbar" />
@ -55,11 +55,12 @@
</feature> </feature>
<preference name="StatusBarOverlaysWebView" value="true" /> <preference name="StatusBarOverlaysWebView" value="true" />
<preference name="StatusBarBackgroundColor" value="#000000" /> <preference name="StatusBarBackgroundColor" value="#000000" />
<preference name="StatusBarStyle" value="lightcontent" />
</config-file> </config-file>
<header-file src="src/ios/CDVStatusBar.h" /> <header-file src="src/ios/CDVStatusBar.h" />
<source-file src="src/ios/CDVStatusBar.m" /> <source-file src="src/ios/CDVStatusBar.m" />
</platform> </platform>
<!-- wp7 --> <!-- wp7 -->

View File

@ -6,9 +6,9 @@
to you under the Apache License, Version 2.0 (the to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@ -17,7 +17,7 @@
under the License. under the License.
*/ */
/* /*
NOTE: plugman/cordova cli should have already installed this, NOTE: plugman/cordova cli should have already installed this,
but you need the value UIViewControllerBasedStatusBarAppearance but you need the value UIViewControllerBasedStatusBarAppearance
in your Info.plist as well to set the styles in iOS 7 in your Info.plist as well to set the styles in iOS 7
@ -34,18 +34,18 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
@property (nonatomic, retain) id sb_hideStatusBar; @property (nonatomic, retain) id sb_hideStatusBar;
@property (nonatomic, retain) id sb_statusBarStyle; @property (nonatomic, retain) id sb_statusBarStyle;
@end @end
@implementation CDVViewController (StatusBar) @implementation CDVViewController (StatusBar)
@dynamic sb_hideStatusBar; @dynamic sb_hideStatusBar;
@dynamic sb_statusBarStyle; @dynamic sb_statusBarStyle;
- (id)sb_hideStatusBar { - (id)sb_hideStatusBar {
return objc_getAssociatedObject(self, kHideStatusBar); return objc_getAssociatedObject(self, kHideStatusBar);
} }
- (void)setSb_hideStatusBar:(id)newHideStatusBar { - (void)setSb_hideStatusBar:(id)newHideStatusBar {
objc_setAssociatedObject(self, kHideStatusBar, newHideStatusBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC); objc_setAssociatedObject(self, kHideStatusBar, newHideStatusBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} }
@ -53,20 +53,20 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
- (id)sb_statusBarStyle { - (id)sb_statusBarStyle {
return objc_getAssociatedObject(self, kStatusBarStyle); return objc_getAssociatedObject(self, kStatusBarStyle);
} }
- (void)setSb_statusBarStyle:(id)newStatusBarStyle { - (void)setSb_statusBarStyle:(id)newStatusBarStyle {
objc_setAssociatedObject(self, kStatusBarStyle, newStatusBarStyle, OBJC_ASSOCIATION_RETAIN_NONATOMIC); objc_setAssociatedObject(self, kStatusBarStyle, newStatusBarStyle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} }
- (BOOL) prefersStatusBarHidden { - (BOOL) prefersStatusBarHidden {
return [self.sb_hideStatusBar boolValue]; return [self.sb_hideStatusBar boolValue];
} }
- (UIStatusBarStyle)preferredStatusBarStyle - (UIStatusBarStyle)preferredStatusBarStyle
{ {
return (UIStatusBarStyle)[self.sb_statusBarStyle intValue]; return (UIStatusBarStyle)[self.sb_statusBarStyle intValue];
} }
@end @end
@ -90,23 +90,22 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
- (void)pluginInitialize - (void)pluginInitialize
{ {
BOOL isiOS7 = (IsAtLeastiOSVersion(@"7.0")); BOOL isiOS7 = (IsAtLeastiOSVersion(@"7.0"));
// init // init
NSNumber* uiviewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"]; NSNumber* uiviewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
_uiviewControllerBasedStatusBarAppearance = (uiviewControllerBasedStatusBarAppearance == nil || [uiviewControllerBasedStatusBarAppearance boolValue]) && isiOS7; _uiviewControllerBasedStatusBarAppearance = (uiviewControllerBasedStatusBarAppearance == nil || [uiviewControllerBasedStatusBarAppearance boolValue]) && isiOS7;
// observe the statusBarHidden property // observe the statusBarHidden property
[[UIApplication sharedApplication] addObserver:self forKeyPath:@"statusBarHidden" options:NSKeyValueObservingOptionNew context:NULL]; [[UIApplication sharedApplication] addObserver:self forKeyPath:@"statusBarHidden" options:NSKeyValueObservingOptionNew context:NULL];
_statusBarOverlaysWebView = YES; // default _statusBarOverlaysWebView = YES; // default
[self initializeStatusBarBackgroundView]; [self initializeStatusBarBackgroundView];
[self styleLightContent:nil]; // match default backgroundColor of #000000
self.viewController.view.autoresizesSubviews = YES; self.viewController.view.autoresizesSubviews = YES;
NSString* setting; NSString* setting;
setting = @"StatusBarOverlaysWebView"; setting = @"StatusBarOverlaysWebView";
if ([self settingForKey:setting]) { if ([self settingForKey:setting]) {
self.statusBarOverlaysWebView = [(NSNumber*)[self settingForKey:setting] boolValue]; self.statusBarOverlaysWebView = [(NSNumber*)[self settingForKey:setting] boolValue];
@ -116,6 +115,11 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
if ([self settingForKey:setting]) { if ([self settingForKey:setting]) {
[self _backgroundColorByHexString:[self settingForKey:setting]]; [self _backgroundColorByHexString:[self settingForKey:setting]];
} }
setting = @"StatusBarStyle";
if ([self settingForKey:setting]) {
[self setStatusBarStyle:[self settingForKey:setting]];
}
} }
- (void) _ready:(CDVInvokedUrlCommand*)command - (void) _ready:(CDVInvokedUrlCommand*)command
@ -124,7 +128,7 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
[self.commandDelegate evalJs:[NSString stringWithFormat:@"StatusBar.isVisible = %@;", [UIApplication sharedApplication].statusBarHidden? @"false" : @"true" ]]; [self.commandDelegate evalJs:[NSString stringWithFormat:@"StatusBar.isVisible = %@;", [UIApplication sharedApplication].statusBarHidden? @"false" : @"true" ]];
} }
- (void) initializeStatusBarBackgroundView - (void) initializeStatusBarBackgroundView
{ {
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
if (UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation)) { if (UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation)) {
@ -146,9 +150,9 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
} }
CGRect bounds = [[UIScreen mainScreen] bounds]; CGRect bounds = [[UIScreen mainScreen] bounds];
if (statusBarOverlaysWebView) { if (statusBarOverlaysWebView) {
[_statusBarBackgroundView removeFromSuperview]; [_statusBarBackgroundView removeFromSuperview];
if (UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation)) { if (UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation)) {
self.webView.frame = CGRectMake(0, 0, bounds.size.height, bounds.size.width); self.webView.frame = CGRectMake(0, 0, bounds.size.height, bounds.size.width);
@ -171,11 +175,11 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
frame.origin.y = statusBarFrame.size.height; frame.origin.y = statusBarFrame.size.height;
frame.size.height -= statusBarFrame.size.height; frame.size.height -= statusBarFrame.size.height;
} }
self.webView.frame = frame; self.webView.frame = frame;
[self.webView.superview addSubview:_statusBarBackgroundView]; [self.webView.superview addSubview:_statusBarBackgroundView];
} }
_statusBarOverlaysWebView = statusBarOverlaysWebView; _statusBarOverlaysWebView = statusBarOverlaysWebView;
} }
@ -190,7 +194,7 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
if (!([value isKindOfClass:[NSNumber class]])) { if (!([value isKindOfClass:[NSNumber class]])) {
value = [NSNumber numberWithBool:YES]; value = [NSNumber numberWithBool:YES];
} }
self.statusBarOverlaysWebView = [value boolValue]; self.statusBarOverlaysWebView = [value boolValue];
} }
@ -204,24 +208,24 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
#pragma clang diagnostic pop #pragma clang diagnostic pop
} }
} }
- (void) setStyleForStatusBar:(UIStatusBarStyle)style - (void) setStyleForStatusBar:(UIStatusBarStyle)style
{ {
if (_uiviewControllerBasedStatusBarAppearance) { if (_uiviewControllerBasedStatusBarAppearance) {
CDVViewController* vc = (CDVViewController*)self.viewController; CDVViewController* vc = (CDVViewController*)self.viewController;
vc.sb_statusBarStyle = [NSNumber numberWithInt:style]; vc.sb_statusBarStyle = [NSNumber numberWithInt:style];
[self refreshStatusBarAppearance]; [self refreshStatusBarAppearance];
} else { } else {
[[UIApplication sharedApplication] setStatusBarStyle:style]; [[UIApplication sharedApplication] setStatusBarStyle:style];
} }
} }
- (void) setStatusBarStyle:(NSString*)statusBarStyle - (void) setStatusBarStyle:(NSString*)statusBarStyle
{ {
// default, lightContent, blackTranslucent, blackOpaque // default, lightContent, blackTranslucent, blackOpaque
NSString* lcStatusBarStyle = [statusBarStyle lowercaseString]; NSString* lcStatusBarStyle = [statusBarStyle lowercaseString];
if ([lcStatusBarStyle isEqualToString:@"default"]) { if ([lcStatusBarStyle isEqualToString:@"default"]) {
[self styleDefault:nil]; [self styleDefault:nil];
} else if ([lcStatusBarStyle isEqualToString:@"lightcontent"]) { } else if ([lcStatusBarStyle isEqualToString:@"lightcontent"]) {
@ -259,7 +263,7 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
if (!([value isKindOfClass:[NSString class]])) { if (!([value isKindOfClass:[NSString class]])) {
value = @"black"; value = @"black";
} }
SEL selector = NSSelectorFromString([value stringByAppendingString:@"Color"]); SEL selector = NSSelectorFromString([value stringByAppendingString:@"Color"]);
if ([UIColor respondsToSelector:selector]) { if ([UIColor respondsToSelector:selector]) {
_statusBarBackgroundView.backgroundColor = [UIColor performSelector:selector]; _statusBarBackgroundView.backgroundColor = [UIColor performSelector:selector];
@ -272,7 +276,7 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
NSScanner* scanner = [NSScanner scannerWithString:hexString]; NSScanner* scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; [scanner setScanLocation:1];
[scanner scanHexInt:&rgbValue]; [scanner scanHexInt:&rgbValue];
_statusBarBackgroundColor = [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0]; _statusBarBackgroundColor = [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
_statusBarBackgroundView.backgroundColor = _statusBarBackgroundColor; _statusBarBackgroundView.backgroundColor = _statusBarBackgroundColor;
} }
@ -283,11 +287,11 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
if (!([value isKindOfClass:[NSString class]])) { if (!([value isKindOfClass:[NSString class]])) {
value = @"#000000"; value = @"#000000";
} }
if (![value hasPrefix:@"#"] || [value length] < 7) { if (![value hasPrefix:@"#"] || [value length] < 7) {
return; return;
} }
[self _backgroundColorByHexString:value]; [self _backgroundColorByHexString:value];
} }
@ -303,40 +307,40 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
[app setStatusBarHidden:YES]; [app setStatusBarHidden:YES];
} }
} }
- (void) hide:(CDVInvokedUrlCommand*)command - (void) hide:(CDVInvokedUrlCommand*)command
{ {
UIApplication* app = [UIApplication sharedApplication]; UIApplication* app = [UIApplication sharedApplication];
if (!app.isStatusBarHidden) if (!app.isStatusBarHidden)
{ {
self.viewController.wantsFullScreenLayout = YES; self.viewController.wantsFullScreenLayout = YES;
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
[self hideStatusBar]; [self hideStatusBar];
if (IsAtLeastiOSVersion(@"7.0")) { if (IsAtLeastiOSVersion(@"7.0")) {
[_statusBarBackgroundView removeFromSuperview]; [_statusBarBackgroundView removeFromSuperview];
} }
if (!_statusBarOverlaysWebView) { if (!_statusBarOverlaysWebView) {
CGRect frame = self.webView.frame; CGRect frame = self.webView.frame;
frame.origin.y = 0; frame.origin.y = 0;
if (UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation)) { if (UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation)) {
frame.size.height += statusBarFrame.size.width; frame.size.height += statusBarFrame.size.width;
} else { } else {
frame.size.height += statusBarFrame.size.height; frame.size.height += statusBarFrame.size.height;
} }
self.webView.frame = frame; self.webView.frame = frame;
} }
_statusBarBackgroundView.hidden = YES; _statusBarBackgroundView.hidden = YES;
} }
} }
- (void) showStatusBar - (void) showStatusBar
{ {
if (_uiviewControllerBasedStatusBarAppearance) { if (_uiviewControllerBasedStatusBarAppearance) {
@ -349,31 +353,31 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
[app setStatusBarHidden:NO]; [app setStatusBarHidden:NO];
} }
} }
- (void) show:(CDVInvokedUrlCommand*)command - (void) show:(CDVInvokedUrlCommand*)command
{ {
UIApplication* app = [UIApplication sharedApplication]; UIApplication* app = [UIApplication sharedApplication];
if (app.isStatusBarHidden) if (app.isStatusBarHidden)
{ {
BOOL isIOS7 = (IsAtLeastiOSVersion(@"7.0")); BOOL isIOS7 = (IsAtLeastiOSVersion(@"7.0"));
self.viewController.wantsFullScreenLayout = isIOS7; self.viewController.wantsFullScreenLayout = isIOS7;
[self showStatusBar]; [self showStatusBar];
if (isIOS7) { if (isIOS7) {
CGRect frame = self.webView.frame; CGRect frame = self.webView.frame;
self.viewController.view.frame = [[UIScreen mainScreen] bounds]; self.viewController.view.frame = [[UIScreen mainScreen] bounds];
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
if (!self.statusBarOverlaysWebView) { if (!self.statusBarOverlaysWebView) {
// there is a possibility that when the statusbar was hidden, it was in a different orientation // 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 // from the current one. Therefore we need to expand the statusBarBackgroundView as well to the
// statusBar's current size // statusBar's current size
CGRect sbBgFrame = _statusBarBackgroundView.frame; CGRect sbBgFrame = _statusBarBackgroundView.frame;
if (UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation)) { if (UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation)) {
frame.origin.y = statusBarFrame.size.width; frame.origin.y = statusBarFrame.size.width;
frame.size.height -= statusBarFrame.size.width; frame.size.height -= statusBarFrame.size.width;
@ -383,19 +387,19 @@ static const void *kStatusBarStyle = &kStatusBarStyle;
frame.size.height -= statusBarFrame.size.height; frame.size.height -= statusBarFrame.size.height;
sbBgFrame.size = statusBarFrame.size; sbBgFrame.size = statusBarFrame.size;
} }
_statusBarBackgroundView.frame = sbBgFrame; _statusBarBackgroundView.frame = sbBgFrame;
[self.webView.superview addSubview:_statusBarBackgroundView]; [self.webView.superview addSubview:_statusBarBackgroundView];
} }
self.webView.frame = frame; self.webView.frame = frame;
} else { } else {
CGRect bounds = [[UIScreen mainScreen] applicationFrame]; CGRect bounds = [[UIScreen mainScreen] applicationFrame];
self.viewController.view.frame = bounds; self.viewController.view.frame = bounds;
} }
_statusBarBackgroundView.hidden = NO; _statusBarBackgroundView.hidden = NO;
} }
} }