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按钮的自定义功能?
- 创建QLPreviewController子类
- 重写- (void)viewWillAppear:(BOOL)animated指定share按钮的响应事件。
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
UIBarButtonItem *shareBtnItem = self.navigationItem.rightBarButtonItem;
if (shareBtnItem) {
[shareBtnItem setAction:@selector(showActivityController)];
}
}
- 实现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];
}