How to hide share button in QLPreviewController using swift?
Asked Answered
R

10

10

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?

Regional answered 28/3, 2016 at 6:14 Comment(2)
Maybe those can help: #22953617 ?Merman
work fine for me https://mcmap.net/q/743035/-how-to-disable-qlpreviewcontroller-print-buttonSatrap
B
5

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.

Balmacaan answered 20/2, 2017 at 17:20 Comment(6)
While this works great on iPad.. the share button on the iPhone is not on the nabber but on the bottom. Any idea how to remove that ? I tried to remove the toolbar button but the share button still remainsFaun
Another solution , that worked great for me, is to create your own VC and to add as a subview the view of the QLPreviewController in it , so you get rid of the share button :)Balmacaan
in ios 11 when users tap navigation bar hides and when thy tap again it appears with share button! Any solution for that?Pompom
Why are you passing url? It seems that it's not used anywhere!Pompom
How to hide it in iOS12.0, for all new AR Quicklook?Lasagne
iOS10.3 ,` navigationItem.rightBarButtonItems?[0] = UIBarButtonItem()` show " Thread 1: Fatal error: Index out of range"Hyperplane
H
5
  1. Create a subclass of QLPreviewController

  2. 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
    }
Hyatt answered 12/4, 2019 at 4:32 Comment(1)
iif U want iOS13 hide rightBarButtonItem need add this navigationItem.rightBarButtonItem = UIBarButtonItem() in " override func viewDidLoad() "Hyperplane
S
3
let previewController = QLPreviewController()
previewController.navigationItem.rightBarButtonItem = UIBarButtonItem()
self.present(previewController, animated: true, completion: { })
Selfpropulsion answered 24/2, 2020 at 9:56 Comment(4)
I think what they are trying to convey is that if you assign rightBarbuttonItem to a new instance of UIBarButtonItem (without any further configuration) it would achieve the result.Ical
IMO this is a great answer!Brabble
doesn't work. If you apply this code then QLPreviewController will show a toolbar at the bottom with the same share button you just removed form the top right of the navigation bar.Farming
This does not work. Tested on iPad (iOS 16.3).Werra
D
2

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.

Doorbell answered 8/2, 2017 at 7:40 Comment(5)
Works for me. Did you find a way to hide the whole toolbar also? This only hides the share icon.Declinometer
There's a post on SO here link. Removes the toolbar but the share button becomes visible. Check it and if you come up with a solution please post your solution as well.Doorbell
it's only an issue in ios 10 it seems, maybe a bug that will be fixed. Before 10 it's hidden.Declinometer
in ios 11 when users tap navigation bar hides and when thy tap again it appears with share button! Any solution for that?Pompom
This solution didnt work for me on iPadOS 16 (iPad).Werra
H
1

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

Hydrodynamics answered 7/11, 2018 at 10:34 Comment(0)
S
1

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)
  • Using this method you will se share button for moments
Shoa answered 12/7, 2020 at 12:26 Comment(3)
6s ,iOS 13.5 ,children[0] will crash, so i change it to children.first, but it seem code not excuted ,and toolbar showHyperplane
doesn't work, the share button is still visible & workingDrysalter
This does not work for me on iPadOS 16.3.Werra
T
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
}
Tonita answered 25/3, 2021 at 2:26 Comment(3)
override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() navigationController?.toolbar.isHidden = true navigationItem.rightBarButtonItem = nil }Tonita
Welcome to SO and thanks for contributing! It's often helpful if you provide some additional context around code snippets explaining what they do and/or how they answer the question. Links to sources or references are also helpful for those trying to adapt your code to slightly different situations.Matron
This answer is ineffective.Andres
R
0

This is the best solution I came up with.

The trick is to set the animation to true when hiding the toolbar. Otherwise we will see the share button shown then disappeared.

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

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        navigationController?.setToolbarHidden(true, animated: true)
    }
}
Rurik answered 14/10, 2021 at 18:11 Comment(0)
U
-1

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

        }
    }
Usage answered 27/9, 2018 at 10:0 Comment(1)
Referencing views using subview indexes is highly discouraged for forward compatibilityVacuum
P
-1

Swift5 elegant solution:

class QLPreviewVC: QLPreviewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let add = self.children.first as? UINavigationController {
            if let navBar = add.view.subviews.compactMap({ $0 as? UINavigationBar }).first {
                navBar.topItem?.rightBarButtonItem?.isHidden = true
            }
        }
    }
}

private extension UIBarButtonItem {
    var isHidden: Bool {
        get {
            return !isEnabled && tintColor == .clear
        }
        set {
            tintColor = newValue ? .clear : nil
            isEnabled = !newValue
        }
    }
}
Privett answered 17/5, 2022 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.