天天看点

iOS系统预览文件但不分享的实用技巧 (iOS10, QLPreviewController)

苹果系统的文件预览能力对于很多app来说是必要的,但其提供的UI相关api并不是可以定制的,比如需要定制toolbar, navigationbar的情况,直接使用QLPreviewController或者

UIDocumentInteractionController 并不能达到想要的效果。

根据笔者的试验来看,现在 iOS10 QLPreviewController 和 UIDocumentInteractionController一样,都是直接在下面显示一个分享按钮,上面是标题栏。

如何对其UI进行定制呢?

QLPreviewController 作为一个正儿八经的viewcontroller,可以对其进行子类化操作,改变其行为,代码类似这样:

 (void)viewWillLayoutSubviews

{

    [super viewWillLayoutSubviews];

    self.navigationController.navigationBar.hidden = YES;

    self.navigationController.toolbar.hidden = YES;

    // custom view demonstrate

  UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];

   topView.backgroundColor = [UIColor redColor];

    [self.view addSubview:topView];

}

为何在viewWillLayoutSubviews执行操作呢?

preview过程中可以点击全屏放大缩小,在这个过程中,你会发现此前所做的view操作被恢复了;这里的viewWillLayoutSubviews相当于是一个hook,可以让我们在view改变以后再把他置回来,达到我们想要的效果。

还有另外一种方案,只add  QLPreviewController的 view 作为subview,代码类似这样:

  MyPreviewViewController *qlVc = [[MyPreviewViewController alloc] init];

        qlVc.delegate = self;

        qlVc.dataSource = self;

    qlVc.view.frame = CGRectMake(0, 100, 300, 300);

    [self.view addSubview:qlVc.view];

对于该frame以外的部分则可以由我们来自由定制。

而UIDocumentInteractionController作为一个nsobject对象,产生的新UI要对其操纵所需要的手法可能更加tricky,这里笔者也没有做更多的实践,如果大家有好的思路也请分享给我,谢谢!

对于以前版本(iOS10以前)的QLPreviewController,分享按钮貌似是在右上角的,对其定制的方案讨论有很多,这里贴出链接供大家参考:

http://blog.csdn.net/jeffasd/article/details/49662483

https://stackoverflow.com/questions/6957091/qlpreviewcontroller-remove-or-add-uibarbuttonitems

http://www.jianshu.com/p/73048dbe6a7d

其思路主要是以下几种:

1. 子类化 ;  2. category, method swizz; 3. 只使用它的view

比我们上面的方案多了一种使用oc runtime的做法;oc中的runtime功能比较强大,有开发者想到这种解决方案也是正常的。

继续阅读