关于UIDocumentInteractionControlle

2018-12-20  本文已影响0人  激动的厨师

关于UIDocumentInteractionController

    UIDocumentInteractionController是苹果很早就有的一个很强大的文档阅读,预览,以及分享到第三方的阅读器。
   最近公司要求做一个文档下载查看功能,我上网查了下资料,就用到了UIDocumentInteractionController,用起来也很简单:

@interface WenDangDetailVC
@property (nonatomic ,strong)UIDocumentInteractionController *document;
@end

- (void)onLookBtn{//查看
    if (self.model.isLocal) {
        
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
        NSString *path = [cachesPath stringByAppendingPathComponent:self.model.FFileUrl];

        self.document = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];

        self.document.delegate = self;
        
        [self.document presentPreviewAnimated:YES];
    
    }else{
        NSURL* url = [[NSURL alloc]initWithString:self.model.FFileUrl];
        [[UIApplication sharedApplication] openURL:url];
    }
}

#pragma mark --UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    
    return self;
}

   需要注意要实现UIDocumentInteractionControllerDelegate这个代理
   这样就可以阅读已经下载的文档了,但是,但是,我发现阅读界面导航栏不能修改!这个阅读器是modal上一个QLPreviewController,但是我们修改不了他的导航栏,很奇怪。如图:

image.png

   怎么办呢?我想到重写presentViewController方法,在这里获取到QLPreviewController来设置导航栏,但是还是没用,囧。
   最后还是解决了,用的是父子控制器,在presentViewController获取到QLPreviewController后:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion{
    LookWenDangVC *vc = [[LookWenDangVC alloc]initWihtVC:viewControllerToPresent model:self.model];
    [self.navigationController pushViewController:vc animated:true];
}

在LookWenDangVC中:

@implementation LookWenDangVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.FName;
    self.navigationItem.leftBarButtonItem = BACK_BUTTON;
    UIButton *rightBtn = [SFButton RightImageButtonItem:@"share_icon"];//
    [rightBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    
    [self addChildViewController:self.detailVC];
    [self.view addSubview:self.detailVC.view];
    [self.detailVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(self.view.mas_top);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
}

这样才实现导航栏的统一如图:


image.png
上一篇下一篇

猜你喜欢

热点阅读