如何使用swift在QLPreviewController中隐藏分享按钮?

我正在使用以下代码使用QLPreviewcontroller在我的应用程序中显示一些文档,

let ql = QLPreviewController() ql.dataSource = self //ql.navigationItem.rightBarButtonItems = nil ql.navigationItem.rightBarButtonItem = nil presentViewController(ql, animated: true, completion: nil) 

我不希望QLPreviewcontroller右上角的分享按钮。 我试过将rightBarButtonItem为nil,但它不起作用。

我怎么能隐藏它?

在适用于iOS 10的Swift 3中 ,这些解决方案都不适用于我。 问题是在viewDidAppear方法之后创建了“共享”按钮。

以下是我删除共享按钮的步骤:

1)Subclassed我的QLPreviewController

2)创建了一个在这个子类中打开我的文档的方法:

 func show(controller: UIViewController, url: NSURL) { // Refreshing the view self.reloadData() // Printing the doc if let navController = controller.navigationController { navController.pushViewController(self, animated: true) } else { controller.show(self, sender: nil) } } 

3)在我的viewDidLayoutSubviews中,我创建了一个虚拟按钮项来替换共享按钮:

  override func viewDidLayoutSubviews() { navigationItem.rightBarButtonItems?[0] = UIBarButtonItem() } 

4)当我想在另一个VC中打开文档时,我称之为:

  QLSubclass().show(controller: self, url: path as NSURL) 

注意:始终以这种方式调用它,而不是使用您实例化的全局变量,因为在它消失之前总是会看到共享按钮。

我知道这是一个古老的问题,但我花了很多时间寻找解决方案,并提出了一些有效的方法。

所以,任何人都在寻找和我一样的东西。 这是我的解决方案。

代码在objective-c中,但它是对Swift的简单转换

首先,我们创建QLPreviewController的子类,并在子类中重写以下方法

编辑

迅速:

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationItem.rightBarButtonItem = nil //For ipads the share button becomes a rightBarButtonItem self.navigationController?.toolbar?.isHidden = true //This hides the share item self.navigationController?.toolbar?.addObserver(self, forKeyPath: "hidden", options: NSKeyValueObservingOptionPrior, context: nil) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.navigationController?.toolbar?.removeObserver(self, forKeyPath: "hidden") } override func observeValue(forKeyPath keyPath: String, ofObject object: Any, change: [AnyHashable: Any], context: UnsafeMutableRawPointer) { var isToolBarHidden: Bool? = self.navigationController?.toolbar?.isHidden // If the ToolBar is not hidden if isToolBarHidden == nil { DispatchQueue.main.async(execute: {() -> Void in self.navigationController?.toolbar?.isHidden = true }) } } self.navigationController?.pushViewController(qlPreviewController, animated: true) 

Objective-C的:

 -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.rightBarButtonItem = nil; //For ipads the share button becomes a rightBarButtonItem [[self.navigationController toolbar] setHidden:YES]; //This hides the share item [[self.navigationController toolbar] addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionPrior context:nil]; } 

删除viewWillDisappear上的Observer

 -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[self.navigationController toolbar] removeObserver:self forKeyPath:@"hidden"]; } 

观察者方法:必需,因为当您单击图像以隐藏导航栏和工具栏时,共享按钮会在点击时再次显示。

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ BOOL isToolBarHidden = [self.navigationController toolbar].hidden; // If the ToolBar is not hidden if (!isToolBarHidden) { dispatch_async(dispatch_get_main_queue(), ^{ [[self.navigationController toolbar] setHidden:YES]; }); } } 

并且必须从现有的navigationController推送PreviewController

 [self.navigationController pushViewController:qlPreviewController animated:YES]; 

而且我们还必须使用子类而不是QLPreviewController。