文档交互控制器与iOS 7状态栏?
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; }
当交互控制器首次出现时,状态栏与标题重叠。
在另一侧旋转到横向可以暂时修复该行为。
正如预期的那样,点击文档本身可以解散框架。 然而,一旦再次轻敲文档以激活帧,则与第一图像一样再次发生重叠。
我试过设置documentInteractionControllerRectForPreview
无济于事。
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller { return CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height); }
我不希望在交互控制器出现时隐藏状态栏,我认为可以正确执行此操作,因为Mail应用程序行为正常并且看起来它使用的是同一个类。
为任何想要使用代码的人附加的最小示例项目: https : //hostr.co/PiluL1VSToVt
我通过将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
虚拟视图控制器允许弹出交互控制器(后退按钮)。
找到新的解决方案
在info.plist文件中为iOS 7添加此项:UIViewControllerBasedStatusBarAppearance(查看基于控制器的状态栏外观)= NO
这些解决方案对我不起作用。 我找到的唯一解决方案是在委托请求呈现视图控制器之后强制状态栏在下一个runloop上可见(需要UIViewControllerBasedStatusBarAppearance也设置为NO):
- (UIViewController *) documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *) controller { // hack to keep status bar visible [[NSOperationQueue mainQueue] addOperationWithBlock: ^{ [[UIApplication sharedApplication] setStatusBarHidden:NO]; }]; return self.viewController; }
尝试下面的代码它适用于我:
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller { [[UIApplication sharedApplication] setStatusBarHidden:YES]; } - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller { [[UIApplication sharedApplication] setStatusBarHidden:YES]; }