91

In iOS 7, Phonegap applications will appear underneath the status bar. This can make it difficult to click on buttons/menus that have been placed at the top of the screen.

Is there someone who knows a way to fix this status bar issue on iOS 7 in a Phonegap application?

I've tried to offset the entire web page with CSS but it doesn't seem to work. Is there a way to like offset the entire UIWebView or just make the status bar behave like it did in iOS6?

Thanks

leech
  • 8,293
  • 7
  • 62
  • 78
Luddig
  • 2,809
  • 3
  • 20
  • 23
  • what exactly is the "status bar issue" ? Any sample code to reproduce? – Raptor Oct 06 '13 at 14:13
  • 1
    @ShivanRaptor With iOS 7, the status bar is now part of the view, so if you have stuff that was at the top of the screen with previous iOS versions, it is now underneath the status bar, not automatically pushed right below it, which can cause some problems. My suggestion would be to create a wrapper for your content, and set a margin-top of 20px. – Andrew Lively Oct 06 '13 at 14:48
  • @AndrewLively - This doesn't really work when the page is scrolling, is there any way to move the UIWebView? – Luddig Oct 06 '13 at 17:53

18 Answers18

142

I found an answer on another thread, but I'll answer the question in case someone else wonders.

Just replace the viewWillAppear in MainViewController.m with this:

- (void)viewWillAppear:(BOOL)animated {
    // View defaults to full size.  If you want to customize the view's size, or its subviews (e.g. webView),
    // you can do so here.
    // Lower screen 20px on ios 7
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        CGRect viewBounds = [self.webView bounds];
        viewBounds.origin.y = 20;
        viewBounds.size.height = viewBounds.size.height - 20;
        self.webView.frame = viewBounds;
    }
    [super viewWillAppear:animated];
}
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
Luddig
  • 2,809
  • 3
  • 20
  • 23
  • 9
    Is anyone else using this with the In App Browser plugin? If I configure the In App Browser as "presentationstyle=fullscreen", a blank bottom margin is appearing after the browser is closed. It looks like the margin is the same height as the InAppBrowser toolbar. Setting the presentationtype to "pagesheet" fixes this on iPad, but iPhone seems to always use "fullscreen". Any workarounds? – Paul Oct 23 '13 at 07:23
  • 1
    Very grateful for the answer -- however the same issue applies to the PhoneGap inApp browser -- it overlays the status bar as well. Any ideas for that? – Michael Oct 25 '13 at 02:03
  • 2
    Yes, we use an older version of Cordova (2.3.0), and to fix this you'll have to set webViewBounds.origin.y = 20; in createViewsWithOptions. Also, see if the background color is being set and play around with that. Do a version check for iOS7 and above, just as in this excellent answer from LudWig Kristoffersson. – hnilsen Oct 28 '13 at 15:52
  • 3
    Also - if you're using the InAppBrowser, the view will re-initialize each time you return. So it would be prudent to add a flag that says it's already been run to your MainViewController.m. – hnilsen Oct 28 '13 at 15:53
  • 14
    Because this code runs in viewWillAppear, you'll run into issues with this solution if your PhoneGap/Cordova app displays other native views (like the camera capture dialog) and returns to this view. The fix for me was to initialize 'viewBounds' to '[self.view bounds]' instead of '[self.webView bounds]'. – Chris Karcher Nov 27 '13 at 01:58
  • by the way I did have an issue with the InAppBrowser, explain in the first comment here. and I applied the recommendation from Chris Karcher above. that worked for me. So thank you. regards, – Carlos G. Dec 13 '13 at 17:23
  • 1
    Shouldn't it actually be - 40 px to viewBounds.size.height in order to compensate the lost pixels? Without this the bottom of page is not visible. – Sampsa Mar 21 '14 at 09:28
  • 1
    I ran into the same thing Chris did. I just put the solution in webViewDidFinishLoad... that way it only gets called once. When I had it in viewWillAppear, the height of the view would shrink 20 pixels every time I tried to use the camera. – bmurmistro Mar 26 '14 at 01:38
  • This will make the view shrink every time the camera is used. – leech Apr 10 '14 at 16:17
  • 1
    I just moved the whole code into viewDidLoad after calling [super viewDidLoad] and it seems to be working perfectly even after popping out to camera and back. – Austin France Jun 13 '14 at 16:20
  • Maybe this page can help someone http://zsprawl.com/iOS/2013/10/fixing-the-phonegap-status-bar-in-ios7/ – magorich Jun 24 '14 at 16:35
  • The problem I have with this is when navigating back I have to scroll down because my pages are 20px up... – Paranoid Android Aug 15 '14 at 14:22
  • Thanks. It worked as expected. Is there a way I can change the color of status bar? It's currently showing as white. (I'm not an iOS developer so code samples will be more than welcome.) Thanks – Umair Aug 29 '14 at 15:10
37

In addition to Ludwig Kristoffersson's resize fix, I recommend changing status bar color:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        CGRect viewBounds = [self.webView bounds];
        viewBounds.origin.y = 20;
        viewBounds.size.height = viewBounds.size.height - 20;
        self.webView.frame = viewBounds;
    }
    self.view.backgroundColor = [UIColor blackColor];
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
Alex L
  • 8,419
  • 6
  • 43
  • 51
  • Perfect - this was the better solution for me. – David Daudelin Mar 20 '14 at 14:35
  • This fix works locally after an Xcode build, but not after I do a phonegap build. Any ideas? I have an ios app built with phonegap 2.9 – DemitryT Apr 30 '14 at 13:12
  • This solution works but I had a problem with fullscreen videos and I found this other one and it fixed my problem http://zsprawl.com/iOS/2013/10/fixing-the-phonegap-status-bar-in-ios7/ – magorich Jun 24 '14 at 16:34
  • @magorich I tried it, but it hides bottom 20px of screen – Kirankumar Sripati Sep 11 '14 at 15:31
  • 1
    @KirankumarSripati actually my last solution was this I hope thiw works for you NSInteger screenSize = 0; //Global if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { CGRect viewBounds = [self.webView bounds]; viewBounds.origin.y = 20; if(screenSize == 0) { viewBounds.size.height = viewBounds.size.height - 20; screenSize = viewBounds.size.height; } self.webView.frame = viewBounds; } I did that because my app was resizing everytime I entered fullscreen – magorich Sep 26 '14 at 00:58
22

please, install that plugin for phonegap: https://build.phonegap.com/plugins/505

And use the correct setting like below, to control the overlay of the webview:

<preference name="StatusBarOverlaysWebView" value="false" />

For me, with Phonegap 3.3.0 it works.

More information, on the Github project page: https://github.com/phonegap-build/StatusBarPlugin

xdemocle
  • 1,337
  • 1
  • 11
  • 13
  • Thanks a lot for this answer!! I was trying with this plugin: https://build.phonegap.com/plugins/715 , the problem is that names are the same. But never works for me. Thanks a lot!! – Jabel Márquez Jun 05 '14 at 03:12
  • To be honest, I don't remember if I tried before this one mentioned by you. – xdemocle Jun 05 '14 at 16:07
20

To hide statusbar, add the following code in the file MainViewController.m under the function -(void)viewDidUnload

- (BOOL)prefersStatusBarHidden
{
    return YES;
}
markmarijnissen
  • 5,569
  • 2
  • 28
  • 34
  • This does the job. It will hide the statusbar making your app full screen. I only tested this on the simulator but will report back later when I run a build on my iPhones and iPads. – Rob Evans Jan 10 '14 at 19:46
  • Works for me on iOS on an iPad 2 – Mark Williams May 02 '14 at 21:45
  • This works fine on iPad Air iOS 7.1.2 and there is no need for any changes or plugins. Great answer, thank you! – Scriptlabs Aug 06 '14 at 20:21
  • This totally ignores everything that Apple says about 'immersive fullscreen mode" in the Human Interface Guidelines... just because you're using a wrapper on top of iOS doesn't mean you can ignore the design patterns... :/ – jesses.co.tt Feb 20 '15 at 09:58
15

Answer https://stackoverflow.com/a/19249775/1502287 worked for me, but I had to change it a bit to make it work with the camera plugin (and potentially others) and a viewport meta tag with "height=device-height" (not setting the height part would cause the keyboard to appear over the view in my case, hiding some inputs along the way).

Each time you would open the camera view and go back to your app, the viewWillAppear method would be called, and your view would shrink by 20px.

Also, the device-height for the viewport would include the 20 extra px, rendering the content scrollable and 20px higher than the webview.

Here is the complete solution for the camera problem:

In MainViewController.h:

@interface MainViewController : CDVViewController
@property (atomic) BOOL viewSizeChanged;
@end

In MainViewController.m:

@implementation MainViewController

@synthesize viewSizeChanged;

[...]

- (id)init
{
    self = [super init];
    if (self) {
        // On init, size has not yet been changed
        self.viewSizeChanged = NO;
        // Uncomment to override the CDVCommandDelegateImpl used
        // _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self];
        // Uncomment to override the CDVCommandQueue used
        // _commandQueue = [[MainCommandQueue alloc] initWithViewController:self];
    }
    return self;
}

[...]

- (void)viewWillAppear:(BOOL)animated
{
    // View defaults to full size.  If you want to customize the view's size, or its subviews (e.g. webView),
    // you can do so here.
    // Lower screen 20px on ios 7 if not already done
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && !self.viewSizeChanged) {
        CGRect viewBounds = [self.webView bounds];
        viewBounds.origin.y = 20;
        viewBounds.size.height = viewBounds.size.height - 20;
        self.webView.frame = viewBounds;
        self.viewSizeChanged = YES;
    }
    [super viewWillAppear:animated];
}

Now for the viewport problem, in your deviceready event listener, add this (using jQuery):

if (window.device && parseFloat(window.device.version) >= 7) {
  $(window).on('orientationchange', function () {
      var orientation = parseInt(window.orientation, 10);
      // We now the width of the device is 320px for all iphones
      // Default height for landscape (remove the 20px statusbar)
      var height = 300;
      // Default width for portrait
      var width = 320;
      if (orientation !== -90 && orientation !== 90 ) {
        // Portrait height is that of the document minus the 20px of
        // the statusbar
        height = document.documentElement.clientHeight - 20;
      } else {
        // This one I found experimenting. It seems the clientHeight
        // property is wrongly set (or I misunderstood how it was
        // supposed to work).
        // I don't know if it is specific to my setup.
        width = document.documentElement.clientHeight + 20;
      }
      document.querySelector('meta[name=viewport]')
        .setAttribute('content',
          'width=' + width + ',' +
          'height=' + height + ',' +
          'initial-scale=1.0,maximum-scale=1.0,user-scalable=no');
    })
    .trigger('orientationchange');
}

Here is the viewport I use for other versions:

<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0" />

And all works well now.

peterh
  • 11,875
  • 18
  • 85
  • 108
dieppe
  • 151
  • 5
  • Any method without Jquery @dieppe – Arjun T Raj Oct 28 '13 at 10:08
  • simply add this code to - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {} method inside CDVCamera.m that solves camera issue also. NSLog(@"CLOSED CAMERA"); [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone]; – Arjun T Raj Oct 28 '13 at 10:13
  • Man, I don't know the words to say thank you! I've been struggling with the 20px shrink problem with camera, and couldn't find the solution until now... – tuks Aug 18 '14 at 14:41
6

Try going into the app's (app name)-Info.plist file in XCode and add the keys

view controller-based status bar appearance: NO
status bar is initially hidden : YES

This works for me without problem.

dk123
  • 18,684
  • 20
  • 70
  • 77
  • For me, this works in the simulator with a local build, but not in the iPad with a remote build. Using PG 3.1 for the build and iOS7 on the simulator and the iPad. – Per Quested Aronsson Dec 21 '13 at 22:05
  • @PerQuestedAronsson You might want to try building it locally on to the iPad. I'm working with an iPad iOS7 with phonegap 3.1 without problem. – dk123 Dec 21 '13 at 23:48
6

For Cordova 3.1+, there is a plugin that deals with the change in behaviour of the status bar for iOS 7+.

This is nicely documented here, including how the status bar can be reverted to its pre-iOS7 state.

To install the plugin, run:

cordova plugin add org.apache.cordova.statusbar

Then add this to config.xml

<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="StatusBarStyle" value="default" />
trejder
  • 17,148
  • 27
  • 124
  • 216
track0
  • 169
  • 2
  • 11
  • You can also set the StatusBar color which defaults to black: See: http://plugins.cordova.io/#/package/org.apache.cordova.statusbar – ezzadeen Jan 15 '15 at 19:06
  • 2
    For Cordova 5+, the plugin has been renamed: cordova plugin add cordova-plugin-statusbar – raider33 Sep 02 '15 at 01:49
5

I solve my problem by installing the org.apache.cordova.statusbar and adding in my top config.xml:

<preference name="StatusBarOverlaysWebView" value="false" /> 
<preference name="StatusBarBackgroundColor" value="#000000" />
<preference name="StatusBarStyle" value="lightcontent" />

These configurations only works for iOS 7+

Reference: http://devgirl.org/2014/07/31/phonegap-developers-guid/

mayconbelfort
  • 195
  • 1
  • 5
  • 17
3

See the new Cordova StatusBar plugin which addresses that exact issue. http://docs.icenium.com/troubleshooting/ios7-status-bar#solution option #3

Philippe Monnet
  • 1,152
  • 1
  • 9
  • 13
1

I use the following piece of code to add a class to the body, if iOS7 is detected. I then style that class to add a 20px margin at the top of my container. Make sure you have the "device" plugin installed and that this code is inside the "deviceready" event.

From reading around, I've heard that the next update to Phonegap (3.1 I believe) will better support the changes to iOS7's status bar. So this may just be needed as a short term fix.

if(window.device && parseFloat(window.device.version) >= 7){
  document.body.classList.add('fix-status-bar');
}
Ryan
  • 3,726
  • 3
  • 26
  • 38
1

Ludwig's answer worked for me.

For anyone who used his answer and is now looking to change the color of the white margin (may seem trivial, but had me stumped), see this:

How can I change the UIWebView background color after sliding down UIWebView to fix the iOS7 status bar overlay issue?

Community
  • 1
  • 1
Paul
  • 529
  • 1
  • 6
  • 20
1

Another way is going backward compatible. Make the HTML according to iOS 7 (with an extra 20px margin on top) to give that full screen look and cut that extra margin off for iOS < 7.0. Something like the following in MainViewController.m:

- (void)viewDidLoad
{
  [super viewDidLoad];

  if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
    CGRect viewBounds = [self.webView bounds];
    viewBounds.origin.y = -20;
    viewBounds.size.height = viewBounds.size.height + 20;
    self.webView.frame = viewBounds;
  }
}

This solution won't leave a black bar on top for iOS 7.0 and above, which a modern iOS user will find odd and old.

tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
0

Write the following code inside AppDelegate.m in didFinishLaunchingWithOptions event (exactly before its last line of code "return YES;" ) :

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) 
{
    [application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
}

I'll wait for your feedback! :)

Ehsan
  • 1,245
  • 11
  • 20
  • This code looks to work great to hide the status bar and fix the issue, i also looked around a bit yesterday and found a peace of code that essentially resized the UIWebView and moved it down I added this as an answer as well :) – Luddig Oct 09 '13 at 19:19
  • That's great. My code is only using a simple and easy way for doing it! All of us are doing the same thing with different methods :) – Ehsan Oct 10 '13 at 07:15
0

if we use camera plugin for image gallery, status bar will come back so to fix that issue please add this line

 [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

to

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {...}

function inside CVDCamera.m in plugins list

Arjun T Raj
  • 3,187
  • 1
  • 21
  • 44
0

Write the following code inside AppDelegate.m in didFinishLaunchingWithOptions event at starting.

CGRect screenBounds = [[UIScreen mainScreen] bounds]; // Fixing status bar ------------------------------- NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."]; if ([[vComp objectAtIndex:0] intValue] >= 7) { // iOS 7 or above CGRect newWebViewBounds = CGRectMake( 0, 20, screenBounds.size.width, screenBounds.size.height-20 ); screenBounds = newWebViewBounds; } // ------------------------------- Fixing status bar End

And fix for above iOS 7 and above.

RahulSalvikar
  • 658
  • 1
  • 10
  • 31
0

To keep the status bar visible in portrait but hide it in landscape (i.e. fullscreen), try the following:

In MainViewController.h:

@interface MainViewController : CDVViewController
@property (atomic) NSInteger landscapeOriginalSize;
@property (atomic) NSInteger portraitOriginalSize;
@end

In MainViewController.m:

@implementation MainViewController

@synthesize landscapeOriginalSize;
@synthesize portraitOriginalSize;

...

- (void)viewWillAppear:(BOOL)animated
{
    // View defaults to full size.  If you want to customize the view's size, or its subviews (e.g. webView),
    // you can do so here.

    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)orientationChanged:(NSNotification *)notification {
    [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}

- (void)viewDidDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
        CGRect frame = self.webView.frame;

        switch (orientation)
        {
            case UIInterfaceOrientationPortrait:
            case UIInterfaceOrientationPortraitUpsideDown:
            {
                if (self.portraitOriginalSize == 0) {
                    self.portraitOriginalSize = frame.size.height;
                    self.landscapeOriginalSize = frame.size.width;
                }
                frame.origin.y = statusBarFrame.size.height;
                frame.size.height = self.portraitOriginalSize - statusBarFrame.size.height;
            }
                break;

            case UIInterfaceOrientationLandscapeLeft:
            case UIInterfaceOrientationLandscapeRight:
            {
                if (self.landscapeOriginalSize == 0) {
                    self.landscapeOriginalSize = frame.size.height;
                    self.portraitOriginalSize = frame.size.width;
                }
                frame.origin.y = 0;
                frame.size.height = self.landscapeOriginalSize;
            }
                break;
            case UIInterfaceOrientationUnknown:
                break;
        }

        self.webView.frame = frame;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    // Change this color value to change the status bar color:
    self.view.backgroundColor = [UIColor colorWithRed:0/255.0f green:161/255.0f blue:215/255.0f alpha:1.0f];
}

This is a combination of what I've found in this and linked StackOverflow discussions, some code from the Cordova StatusBar plugin (so as not to hardcode the 20px value), and some incantations on my part (I'm not an iOS dev so I fumbled my way to this solution).

matb33
  • 2,820
  • 1
  • 19
  • 28
0

First of all, Add the Device plugin in you project. Plugin Id is: org.apache.cordova.device and repository is: https://github.com/apache/cordova-plugin-device.git

After that use this function and call it on every page or screen:-

function mytopmargin() {
    console.log("PLATform>>>" + device.platform);
    if (device.platform === 'iOS') {
        $("div[data-role='header']").css("padding-top", "21px");
        $("div[data-role='main']").css("padding-top", "21px");
    } else {
        console.log("android");
    }
}
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Anil Singhania
  • 785
  • 10
  • 9
0

The best way to control Status Bar background - 2017

After ongoing frustration, a lot of search on the Internet, these are the steps you need to take:

  1. Make sure that you use Status Bar Plugin
  2. Add this setting to your config.xml:

< preference name="StatusBarOverlaysWebView" value="false" />

  1. Use the following code on onDeviceReady

        StatusBar.show();
        StatusBar.overlaysWebView(false);
        StatusBar.backgroundColorByHexString('#209dc2');
    

It's works for me in iOS & Android.

Good luck!

Niv Asraf
  • 267
  • 1
  • 10