I\'m using the below code to use QLPreviewcontroller to show some documents in my app,
let ql = QLPreviewController()
ql.dataSource = self
//ql.navigationIte
I know this is an old question but I spend so many hours looking for a solution and came up with something that works.
So, for any one looking for the same thing as me. Here's my solution.
Codes are in objective-c but it'l be a simple conversion to Swift
First we create subclass of QLPreviewController and in the subclass override the following methods
Edit
Swift:
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];
}
Remove the Observer on viewWillDisappear
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[self.navigationController toolbar] removeObserver:self forKeyPath:@"hidden"];
}
And the observer method: Required because when you single tap the image to hide the navigation bar and toolbar, the share button becomes visible again on tap.
- (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];
});
}
}
And the PreviewController has to be pushed from you existing navigationController
[self.navigationController pushViewController:qlPreviewController animated:YES];
And also we have to use the subclass instead of QLPreviewController.
Swift 5 Solution:
Subclass 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)
}
}
then present this subclass where you want like this:
let previewController = QLVideoController()
present(controller, animated: true, completion: nil)
Create a subclass of QLPreviewController
Add the following code to it
Swift:
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
}
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.
If Still, anyone wants to remove share option or want to customize the Navigation bar of QLPreviewController
then they can try creating a UIViewController
and customize it as per requirement and then create QLPreviewController
object and add it as a child view controller.
This will allow you to get rid of share button and you customize navigation bar color etc. this is working for me.
To know, how to add child view controller you can refer to this
this work for me
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
}
}