需求是在线预览PDF、DOC等文档,经过反复测试。发现无论是UIDocumentInteractionController 和QLPreviewController 均无法实现在线直接预览文件(如果谁做到了望通知),因此折中先实现文件下载,再用QLPreviewController打开,但是打开过程中发现底部有一条ToolBar,上面存在了一个shareItem,因此想要隐藏它。
为了隐藏这个toolBar找到stackover上文章QLPreviewController hide bottom toolbar
新建 QLPreviewController
通过方法获取所有toolBar,测试是2个
-(NSArray *)findAllToolBarsFromView:(UIView *)view{
NSMutableArray * bars = [[NSMutableArray alloc]initWithCapacity:0];
for (UIView * view2 in view.subviews) {
if ([view2 isKindOfClass:[UIToolbar class]]) {
[bars addObject:view2];
}
[bars addObjectsFromArray:[self findAllToolBarsFromView:view2]];
}
return bars;
}```
* 获取到toolBars之后,隐藏toolbar,并监听‘hidden’
```-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.qlToolBar = [self getToolBarFromView:self.view];
self.qlToolBar.hidden = true;
if (self.qlToolBar) {
[self.qlToolBar addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionPrior context:nil];
} }```
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController removeObserver:self forKeyPath:@"hidden"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
BOOL isToolBarHidden = self.qlToolBar.hidden;
// If the ToolBar is not hidden
if (!isToolBarHidden) {
dispatch_async(dispatch_get_main_queue(), ^{
self.qlToolBar.hidden = true;
});
}
}```
最终隐藏了下面的toolBar