使用iOS 7状态栏的文档交互控制器?

6

UIDocumentInteractionController 似乎在横向方向上与新的 iOS 7 状态栏交互存在问题。目前我用于显示查看器的代码如下:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:filePath];

    UIDocumentInteractionController *pdfViewer = [UIDocumentInteractionController interactionControllerWithURL:url];
    [pdfViewer setDelegate:self];
    [pdfViewer presentPreviewAnimated:YES];
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return self;
}

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
    return self.view;
}

当交互控制器首次出现时,状态栏会重叠标题。

enter image description here

在另一侧旋转至横屏模式可以暂时解决此问题。

enter image description here

正如预期的那样,点击文档本身可以关闭框架。然而,一旦再次点击文档以激活框架,就会再次出现重叠,就像第一张图片一样。
我尝试设置documentInteractionControllerRectForPreview,但无效。
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
    return CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height);
}

我不希望在交互控制器出现时隐藏状态栏,我认为这是可能做到正确的,因为邮件应用程序行为正确,而且看起来它正在使用相同的类。
附有最小示例项目供任何想要尝试代码的人使用: https://hostr.co/PiluL1VSToVt

iOS 7有bug吗?有新的解决方案吗?我用了同样的方法来解决问题,但它在我的应用程序中引起了另一个bug。 - GxocT
4个回答

0

尝试下面的代码,对我很有效:

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

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

0

这些解决方案对我无效。我找到的唯一解决方案是,在代理请求呈现视图控制器后,强制在下一个运行循环中显示状态栏(还需要将UIViewControllerBasedStatusBarAppearance设置为NO):

- (UIViewController *) documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *) controller {
    // hack to keep status bar visible
    [[NSOperationQueue mainQueue] addOperationWithBlock:
     ^{
         [[UIApplication sharedApplication] setStatusBarHidden:NO];
     }];
    return self.viewController;
}

0

我通过将UIDocumentInteractionController包装在UINavigationController中,并将应用程序窗口的根视图控制器切换为导航控制器进行演示来解决了这个问题。在我的使用中,其他视图控制器没有使用UINavigationController,因此在解除演示时我们将旧的根控制器切换回去:

#import "MainViewController.h"

@interface MainViewController ()

@property (nonatomic, strong) UINavigationController *navController;
@property (nonatomic, strong) MainViewController *main;

@end

@implementation MainViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.main = self;
    self.navController = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
    [[UIApplication sharedApplication].keyWindow setRootViewController:self.navController];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:filePath];

    UIDocumentInteractionController *pdfViewer = [UIDocumentInteractionController interactionControllerWithURL:url];
    [pdfViewer setDelegate:self];
    [pdfViewer presentPreviewAnimated:YES];
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return self.navController;
}

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [[UIApplication sharedApplication].keyWindow setRootViewController:self.main];
    self.main = nil;
}

- (void)dismiss
{
    [self.navController popViewControllerAnimated:YES];
}

@end

虚拟视图控制器允许交互控制器触发返回按钮。

0
发现新的解决方案。
在info.plist文件中添加以下内容以适用于iOS 7: UIViewControllerBasedStatusBarAppearance(基于视图控制器的状态栏外观)= NO

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接