我正在使用一个需要查看/共享PDF文件的React Native应用程序.我使用的是使用UIDocumentInteractionController的
react-native-open-file模块来查看PDF文件.打开PDF文件时,PDF上将显示状态栏.我的应用程序始终保持隐藏.查看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]; }
更新:
解决方法
另一个黑客解决方案:
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]; }
您可以将定时器定义放在任何地方,只要您关闭预览,请确保无效.我也注意到,如果你在if子句中放置了setStatusBarHidden:YES,那么你检查它是否真的被隐藏,这个解决方案就不再有效了.这似乎是UIDocumentInteractionController中的一个错误.