【问题标题】:Hide status bar when using UIDocumentInteractionController?使用 UIDocumentInteractionController 时隐藏状态栏?
【发布时间】:2017-08-03 21:25:08
【问题描述】:

我正在开发一个需要查看/共享 PDF 文件的 React Native 应用程序。我正在使用react-native-open-file 模块,它使用 UIDocumentInteractionController 来查看 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];
}

更新:

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

【问题讨论】:

  • 使用 setStatusBarHidden 永远隐藏状态栏?
  • @SunilPrajapati 是的,我根本不想看到状态栏。你问的是这个吗?
  • 请看this answer
  • @SunilPrajapati 添加您链接到的更改后,是否需要对上述代码进行任何更改?
  • 不,不需要对上述代码进行任何其他更改。对于整个应用程序隐藏状态栏

标签: ios objective-c react-native


【解决方案1】:

我认为下面的代码应该这样做:

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

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

【讨论】:

  • 似乎工作得很好,谢谢!我想我现在不需要documentInteractionControllerDidEndPreview 代码吧?此外,这会在具有状态栏的应用程序上显示状态栏吗?如果它同时处理这两个问题,我将向 react-native-open-file 模块提交一个拉取请求,以在将来帮助其他人。
  • 可能在最后预览你应该把状态栏放回去
  • 我刚刚意识到,使用上面的代码,当查看文档并从共享按钮(带有向上箭头的方形图标)中选择任何选项(例如打印)时,没有任何反应。如果我把代码放回原来的样子,它就可以正常工作。有什么想法吗?
  • 是的,很有趣!让我检查一下
  • 谢谢@manishg。几天来我一直在为这个问题苦苦挣扎,我有限的 Objective-c 技能让我失望了,如果你有任何其他想法,请告诉我。
【解决方案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 中的一个错误。

【讨论】:

  • 这是目前最好的解决方案,但你说得对,它有点笨拙,当我退出 UIDocumentInteractionController 时状态栏会闪烁一秒钟。关于如何不让它闪烁的任何想法?
  • @TomKrones,即使您使用单独的 UIWindow 显示文档,状态栏在关闭时仍会闪烁:[此外,使用带有 windowLevel == UIWindowLevelStatusBar 的另一个窗口会阻止共享选项工作,因为您有注意到。如果我有什么想法,我会编辑我的答案。
【解决方案3】:

请在您的项目中添加以下代码和配置并检查它。

Info.plist 中将View controller-based status bar appearance 设置为NO

并在 AppDelegate 方法中设置 statusBarHidden

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

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

希望它的工作。

【讨论】:

  • 谢谢!但是你在目标c中有它吗?我只需要UIApplication.sharedApplication().statusBarHidden = true 行。 :)
  • 从头开始。明白了:[UIApplication sharedApplication].statusBarHidden = YES;
  • 对我不起作用。我仍然在文档预览上看到状态栏。检查我的问题,我添加了问题的照片。
  • 问题是没有隐藏整个应用的状态栏。问题是当我打开 PDF 的预览时,会出现状态栏。状态栏仅在预览 PDF 时显示,其余时间状态栏没有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 2011-04-29
  • 2014-11-29
  • 1970-01-01
相关资源
最近更新 更多