8

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

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

我不希望 QLPreviewcontroller 右上角的共享按钮。我试过设置rightBarButtonItem为零,但它不起作用。

我该如何隐藏它?

4

9 回答 9

6

Swift 3 for iOS 10中,这些解决方案都不适合我。问题是 Share 按钮是在 viewDidAppear 方法之后创建的。

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

1) 子类化我的 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)

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

于 2017-02-20T17:20:21.400 回答
4
  1. 创建一个子类QLPreviewController

  2. 将以下代码添加到它

迅速:

var toolbars: [UIView] = []

var observations : [NSKeyValueObservation] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.setRightBarButton(UIBarButtonItem(), animated: false)

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        navigationController?.toolbar.isHidden = true

        if let navigationToobar = navigationController?.toolbar {
            let observation = navigationToobar.observe(\.isHidden) {[weak self] (changedToolBar, change) in

                if self?.navigationController?.toolbar.isHidden == false {
                     self?.navigationController?.toolbar.isHidden = true
                }
            }
            observations.append(observation)
        }

        toolbars = toolbarsInSubviews(forView: view)

        for toolbar in toolbars {

            toolbar.isHidden = true

            let observation = toolbar.observe(\.isHidden) { (changedToolBar, change) in
                if let isHidden = change.newValue,
                    isHidden == false {
                    changedToolBar.isHidden = true
                }
            }

            observations.append(observation)
        }
    }

    private func toolbarsInSubviews(forView view: UIView) -> [UIView] {

        var toolbars: [UIView] = []

        for subview in view.subviews {
            if subview is UIToolbar {
                toolbars.append(subview)
            }
            toolbars.append(contentsOf: toolbarsInSubviews(forView: subview))
        }
        return toolbars
    }
于 2019-04-12T04:32:14.520 回答
3
let previewController = QLPreviewController()
previewController.navigationItem.rightBarButtonItem = UIBarButtonItem()
self.present(previewController, animated: true, completion: { })
于 2020-02-24T09:56:40.680 回答
2

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

因此,对于任何与我寻找相同事物的人。这是我的解决方案。

代码在 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)

目标-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 上的观察者

-(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];
        });
    }
}

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

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

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

于 2017-02-08T07:40:24.527 回答
1

如果仍然有任何人想要删除共享选项或想要自定义导航栏,QLPreviewController那么他们可以尝试创建一个UIViewController并根据要求对其进行自定义,然后创建QLPreviewController对象并将其添加为子视图控制器。

这将使您摆脱共享按钮并自定义导航栏颜色等。这对我有用。

要知道如何添加子视图控制器可以参考这个

于 2018-11-07T10:34:55.297 回答
1

斯威夫特 5 解决方案:

子类 QLPreviewController:

final class CustomQLPreviewController: QLPreviewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = UIBarButtonItem()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        (children[0] as? UINavigationController)?.setToolbarHidden(true, animated: false)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        (children[0] as? UINavigationController)?.setToolbarHidden(true, animated: false)
    }
}

然后在你想要的地方展示这个子类:

let previewController = QLVideoController()
present(controller, animated: true, completion: nil)
  • 使用此方法,您将暂时看到分享按钮
于 2020-07-12T12:26:17.203 回答
0

这是我想出的最佳解决方案。

true诀窍是在隐藏工具栏时将动画设置为。否则,我们将看到显示的共享按钮然后消失。

final class NoSharePreviewController: QLPreviewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = UIBarButtonItem()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        navigationController?.setToolbarHidden(true, animated: true)
    }
}
于 2021-10-14T18:11:10.580 回答
0
override func viewDidLoad() {
    super.viewDidLoad()
    /* Move "Share" Button to bottom */
    navigationItem.rightBarButtonItem = UIBarButtonItem()
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    /* Hide toolbar to hide "Share" button  */
    self.navigationController?.toolbar.isHidden = true
}
于 2021-03-25T02:26:37.640 回答
-1

这对我有用

   class QLSPreviewController : QLPreviewController {

        override func viewDidLoad() {
            super.viewDidLoad()



        }
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)

        }
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(true )
            //This hides the share item
            let add =  self.childViewControllers.first as! UINavigationController
            let layoutContainerView  = add.view.subviews[1] as! UINavigationBar
             layoutContainerView.subviews[2].subviews[1].isHidden = true

        }
    }
于 2018-09-27T10:00:14.247 回答