12

I'm working on an React Native app that needs to view/share PDF files. I'm use the react-native-open-file module which uses the UIDocumentInteractionController to view PDF files. When the PDF file is opened the status bar appears over the PDF. My app has the staus bar hidden at all times. How do I hide the status bar when viewing the PDF?

Here's the code from the module:

//
//  RNDocumentInteractionController.m
//  RNDocumentInteractionController
//
//  Created by Aaron Greenwald on 7/5/16.
//  Copyright © 2016 Wix.com. All rights reserved.
//

#import "RNDocumentInteractionController.h"
#import <UIKit/UIKit.h>

@implementation RNDocumentInteractionController

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(open: (NSURL *)path)
{
    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:path];
    interactionController.delegate = self;
    [interactionController presentPreviewAnimated:YES];
}

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
    return [[[[UIApplication sharedApplication] delegate] window] rootViewController];
}


@end

I was able to add a documentInteractionControllerDidEndPreview method that hides the status after it closes but I would rather never have the status bar open at all:

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

Update:

Here's a picture of the status bar over the menu bar:

Here's a picture of the status bar over the menu bar

Dev01
  • 13,292
  • 19
  • 70
  • 124

3 Answers3

5

I think the below code should do:

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
    [[[[UIApplication sharedApplication] delegate] window] setWindowLevel:UIWindowLevelStatusBar];

    return [[[[UIApplication sharedApplication] delegate] window] rootViewController];
}
manishg
  • 9,520
  • 1
  • 16
  • 19
  • 1
    Seems to work great, thanks! I guess I don't need the `documentInteractionControllerDidEndPreview` code now right? Also, will this show the status bar on apps that have a status bar? If it handles both I'll submit a pull request to the react-native-open-file module to help other people in the future. – Dev01 Mar 17 '17 at 12:26
  • Probably in the end preview you should set back the status bar – manishg Mar 17 '17 at 12:27
  • I just realized that with the code above, when viewing a document and I select any of the options from the share button (the square icon with the arrow pointing up) for example print, nothing happens. If I put the code back to the way it was it works fine. Any ideas? – Dev01 Mar 21 '17 at 01:44
  • Yeah very interesting ! Let me check – manishg Mar 21 '17 at 02:30
  • Thanks @manishg. I've been struggling with this issue for days and my limited objective-c skills are failing me, let me know if you have any other ideas. – Dev01 Mar 21 '17 at 20:19
2

Another hacky solution:

static NSTimer* timer = nil;
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }];
}

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [timer invalidate];
}

You can put the timer definition anywhere you want, just make sure to invalidate it once you close the preview. I have also noticed that if you put the line with setStatusBarHidden:YES inside an if clause where you check if it is actually hidden, this solution no longer works. It seems like a bug in UIDocumentInteractionController.

pckill
  • 3,709
  • 36
  • 48
  • This is the best solution yet but you're right it is a little hacky and the status bar flashes for a second when I exit the UIDocumentInteractionController. Any ideas on how to not make it flash? – Dev01 Mar 22 '17 at 18:03
  • @TomKrones, even if you use a separate UIWindow to show the document, the status bar still flashes on closing :[ Also, using another window with `windowLevel == UIWindowLevelStatusBar` prevents share options from working, as you have noticed. I'll edit my answer if I come up with something. – pckill Mar 23 '17 at 10:29
1

Please add below code and configuration in your project and check it.

In Info.plist set View controller-based status bar appearance to NO

And set statusBarHidden in AppDelegate method

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    UIApplication.sharedApplication().statusBarHidden = true
    return true
}

For whole application hide the status bar. Please comment your code and check.

Hope it's Work.

Sunil Prajapati
  • 473
  • 5
  • 17
  • Thanks! but do you have it in objective c? I just need the `UIApplication.sharedApplication().statusBarHidden = true` line. :) – Dev01 Mar 22 '17 at 14:48
  • scratch that. Got it: `[UIApplication sharedApplication].statusBarHidden = YES;` – Dev01 Mar 22 '17 at 14:53
  • Didn't work for me. I'm still seeing the status bar over the document preview. Check my question, I added a photo of the problem. – Dev01 Mar 22 '17 at 15:00
  • The problem is not hiding the status bar for the whole app. The problem is when I open a preview of a PDF then the status bar appears. The status bar only shows up when previewing the PDF, the rest of the time the status bar isn't a problem. – Dev01 Mar 22 '17 at 15:43