How to hide share button in QLPreviewController using swift?

Nazik picture Nazik · Mar 28, 2016 · Viewed 8.1k times · Source

I'm using the below code to use QLPreviewcontroller to show some documents in my app,

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

I don't want the share button in the right top of QLPreviewcontroller. I'd tried setting rightBarButtonItem to nil, but it's not working.

How can I hide that?

Answer

Ugo Marinelli picture Ugo Marinelli · Feb 20, 2017

None of those solutions worked for me in Swift 3 for iOS 10. The issue is that the Share button is created after viewDidAppear method.

Here are the steps I followed to remove the share button :

1) Subclassed my QLPreviewController

2) Created a method to open my document in this subclass :

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) In my viewDidLayoutSubviews, I created a dummy button item to replace the share button :

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

4) When I want to open a document in another VC, I call it like this :

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

Note : Always call it this way, and not with a global variable you instantiated because you will always see the share button before it disappears.