QLPreviewController自定义分享按钮功能

QLPreviewController是iOS 提供的文档预览功能。
查看文件类型如下:
A QLPreviewController can display previews for the following items:

  • iWork documents
  • Microsoft Office documents (Office ‘97 and newer)
  • Rich Text Format (RTF) documents
  • PDF files
  • Images
  • Text files whose uniform type identifier (UTI) conforms to the public<wbr>.text type (see Uniform Type Identifiers Reference)
  • Comma-separated value (csv) files
  • 3D models in USDZ format (with both standalone and AR views for viewing the model)

如果在navigationController中显示,可以自定义navigation bar上的按钮。
但是当你指定rightBarButtonItem时,原来右上角的分享按钮会自动显示到左下角,并不是隐藏。

如何实现share按钮的自定义功能?

  1. 创建QLPreviewController子类
  2. 重写- (void)viewWillAppear:(BOOL)animated指定share按钮的响应事件。
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UIBarButtonItem *shareBtnItem = self.navigationItem.rightBarButtonItem;
    if (shareBtnItem) {
        [shareBtnItem setAction:@selector(showActivityController)];
    }
}
  1. 实现showActivityController方法。
- (void)showActivityController{
    NSURL *previewItemURL = self.currentPreviewItem.previewItemURL;
    NSArray *items = @[previewItemURL];
    UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:@[]];
    vc.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {};
    [self presentViewController:vc animated:YES completion:NULL];
}

源码:https://github.com/bianfu/BFUIKit

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。