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

时间:2016-03-28 06:14:53

标签: ios swift qlpreviewcontroller rightbarbuttonitem

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

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

我不想要QLPreviewcontroller右上角的分享按钮。我尝试将rightBarButtonItem设置为nil,但它无效。

我该如何隐藏?

8 个答案:

答案 0 :(得分:4)

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

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

1)Subclassed my 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)

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

答案 1 :(得分:3)

let previewController = QLPreviewController()
previewController.navigationItem.rightBarButtonItem = UIBarButtonItem()
self.present(previewController, animated: true, completion: { })

答案 2 :(得分:1)

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

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

代码在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上的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。

答案 3 :(得分:1)

Swift 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)
  • 使用此方法,您会暂时共享按钮

答案 4 :(得分:0)

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

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

要了解如何添加子视图控制器,您可以参考this

答案 5 :(得分:0)

  1. 创建QLPreviewController

  2. 的子类
  3. 向其中添加以下代码

快捷键:

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
    }

答案 6 :(得分: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
}

答案 7 :(得分:-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

        }
    }