使用UIDocumentInteractionController时隐藏状态栏?

时间:2017-03-13 18:24:16

标签: ios objective-c react-native

我正在处理需要查看/共享PDF文件的React Native应用。我使用react-native-open-file模块使用UIDocumentInteractionController来查看PDF文件。打开PDF文件时,状态栏将显示在PDF上。我的应用程序始终隐藏着staus栏。查看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

我能够添加一个documentInteractionControllerDidEndPreview方法,该方法在关闭后隐藏状态,但我宁愿根本不打开状态栏:

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

更新

这是菜单栏上状态栏的图片:

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

3 个答案:

答案 0 :(得分:4)

我认为以下代码应该:

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

    return [[[[UIApplication sharedApplication] delegate] window] rootViewController];
}

答案 1 :(得分:2)

另一个hacky解决方案:

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];
}

您可以将timer定义放在任何您想要的位置,只需确保在关闭预览后使其无效。我还注意到,如果你将setStatusBarHidden:YES行放在if子句中,检查它是否实际上是隐藏的,那么这个解决方案就不再有用了。这似乎是UIDocumentInteractionController中的一个错误。

答案 2 :(得分:1)

请在项目中添加以下代码和配置并进行检查。

Info.plist设置View controller-based status bar appearanceNO

并在AppDelegate方法中设置statusBarHidden

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

对于整个应用程序隐藏状态栏。 请评论您的代码并检查。

希望它有效。

相关问题