Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide share button in QLPreviewController using swift?

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?

like image 502
Nazik Avatar asked Apr 23 '26 14:04

Nazik


1 Answers

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.

like image 157
Ugo Marinelli Avatar answered Apr 25 '26 06:04

Ugo Marinelli