最近需求中有加载文档类要求,发现苹果提供 QLPreviewController 可以很方便的加载这些内容,但是 QLPreviewController 对于导航条好像不能做太多操作,我们可以新建一个 viewController 并把 QLPreviewController 添加上,这样可以方便自己自定义很多内容,具体代码如下
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface BankQlLoadViewController : UIViewController
@property (nonatomic,copy) NSString *pathStr;
@property (nonatomic, copy) NSString *pathTitle;
@end
NS_ASSUME_NONNULL_END
#import "BankQlLoadViewController.h"
#import <QuickLook/QuickLook.h>
@interface BankQlLoadViewController ()<QLPreviewControllerDataSource>
@end
@implementation BankQlLoadViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = self.pathTitle;
[self configViews];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)configViews{
// 将QLPreviewControler添加到本控制器上
QLPreviewController *QLController = [[QLPreviewController alloc] init];
QLController.dataSource = self;
[self addChildViewController:QLController];
[QLController didMoveToParentViewController:self];
[self.view addSubview:QLController.view];
QLController.view.frame = CGRectMake(0,64, self.view.frame.size.width, self.view.frame.size.height-64);
}
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
return 1;
}
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
NSURL *url = [NSURL fileURLWithPath:self.pathStr];
return url;
}
@end
使用还是比较简单方便的,我是直接加载的本地文件。
网友评论