9

Can anyone tell me how to remove the QLPreviewController print button? Also would like to disable cut/paste/copy.

rbrown
  • 2,635
  • 2
  • 24
  • 24
  • For a solution for iOS6, look here: http://stackoverflow.com/questions/13083546/qlpreviewcontroller-hide-print-button-in-ios6 – Krumelur Nov 13 '12 at 12:12
  • work fine for me using Xcode 8.3 https://stackoverflow.com/a/45344701/1603380 – Buntylm Jul 27 '17 at 08:00

9 Answers9

6

UPDATE:

This no longer works in iOS 6. Quick Look runs in another process using XPC. See [here][3] for more details. I don't foresee any way to customize QLPreviewController. The following answer remains for anyone interested for pre-iOS 6.


If you want to simply remove the action button you need to subclass QLPreviewController. Then in -viewWillAppear: you need to remove the action button by calling [[self navigationItem] setRightBarButtonItem:nil]; This will also remove the ability to share files with other apps. If you are fine with that loss, then that's the easiest solution. One thing to be aware of is that QLPreviewController is NOT intended to be customized. You can check out this repository on Github. It contains a QLPreviewController that is already safely subclassed. You just need to add the one line to remove the action button. The repo also has some other conveniences.

The better but more complicated solution is to use UIDocumentInteractionController. From what I understand of QLPreviewController is that it's built using UIDocumentInteractionController. QLPreviewController was made for general use and easy addition. UIDocumentInteractionController gives much more control, but is harder to use. I can't sufficiently describe how to use it here. I recommend checking out WWDC 2010 session 106 Understanding Document Interaction Controller.

As for disabling cut/copy/paste, you can't do that with QLPreviewController. You might be able to do that with UIDocumentInteractionController, but I wouldn't count on it. Depending on what files you want to display, you may be able to do a completely custom implementation, but that's a lot of work. It's relatively easy to make viewers for plain text, photos, videos, and PDFs. Office documents are more effort than it's worth.

EDIT:

I've built the ability to remove the action button right into RBFilePreviewer so you don't have to worry about doing it yourself.

rbrown
  • 2,635
  • 2
  • 24
  • 24
  • Thanks. Only worked when I setRightBarButtonItem:nil in "viewDidAppear" – ricardo somarriba Aug 05 '11 at 15:57
  • 1
    -viewDidAppear is an odd place to put that. Does it work in -viewWillAppear? That would be much better if possible. Also, are you going to upvote/accept my answer as correct? – rbrown Aug 05 '11 at 18:00
  • 1
    It turns out that removing the action button isn't as trivial as I thought. QLPreviewController has a lot of quirks underneath. RBFilePreviewer now has that feature built in and takes care of all the different cases. Plus, I fixed a "bug" in the original QLPreviewController that causes it to not show the document navigation bar when pushed instead of being presented modally. You can also see a demo of RBFilePreviewer in my demo repository: https://github.com/rob-brown/Demos. – rbrown Aug 05 '11 at 22:39
  • @rbrown: I have started to test QuickLook framework and I'm experiencing weird leak when testing on iPhone 3GS + iOS4.0. The leak appears even when using pure framework. However, no leak appears when using iPhon 4 + iOS 5.0. Recently, I have posted the problem http://stackoverflow.com/questions/7992308/strange-leak-when-using-quicklook-framework-in-ios4-0 It would be interesting if you could test that leak too :) – Centurion Nov 03 '11 at 09:26
  • 2
    RBFilePreviewer no longer works as of iOS6 :( At least, not for the removing buttons part... – DOOManiac Oct 06 '12 at 23:16
  • 1
    It does not work for iOS 8 GM Seed. Some other suggestions please. – Meet Sep 11 '14 at 10:45
2
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self navigationItem].rightBarButtonItems = nil;
}

Works for me instead of [[self navigationItem] setRightBarButtonItem:nil];

Andrea Leganza
  • 447
  • 7
  • 7
  • Works for me too but the arrows next and prev button have curiously disappear leaving two black empty button. – Gonzo Oin Aug 22 '12 at 11:48
2

If you subclass QLPreviewController and then add this one method:

-(void)viewDidAppear:(BOOL)animated{

    [[self navigationItem] setRightBarButtonItem:nil]; 
}

in the subclass, the action button will disappear as rbrown notes above. This will not work if you use viewWillAppear. Also, an unexpected side-effect of doing this is that the toolbar on the preview now appears at all times, instead of just when you tap the view.

Flexo
  • 87,323
  • 22
  • 191
  • 272
millport
  • 2,411
  • 5
  • 23
  • 19
  • This does not work, as well as previous answer by Neogene Sparanza, in case you have many documents to preview - everything becomes like before this after you change the document. – Eugene Dudnyk Apr 24 '14 at 14:43
1

I also need to custom the navigaiton item of QLPreviewController. Just like rbrown said, XPC is used and we can no longer add custom items in viewDidLoad or viewWillAppear.

Currently I use this way:

  1. create a new class of UIViewController
  2. add the view of QLPreviewController to the new class

It seems wired but works.

Community
  • 1
  • 1
scorpiozj
  • 2,687
  • 5
  • 34
  • 60
0

I solve this problem in this way:

UIView *viewPreviewController; //from storyboard

QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.delegate = self;
previewController.dataSource = self;
previewController.currentPreviewItemIndex = 0;
[previewController.view setFrame:CGRectMake(0,0, self.viewPreviewController.frame.size.width, self.viewPreviewController.frame.size.height)];
[previewController.navigationController setHidesBarsOnTap:YES];
previewController.navigationItem.rightBarButtonItems = nil;

[self.viewPreviewController addSubview:previewController.view];

If i had not incorporate the previewController in a UIView, then it would not work!

Alessio Campanelli
  • 970
  • 11
  • 19
0

I have found a solution to disable therightBarButtonItem in QLPreviewController that works fine for me in iOS8 and iOS9

You simply need to subclass QLPreviewController and override the following methods, then use your subclass instead of the original QLPreviewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // When coming back from background we make sure the share button on the rightbBarButtonItem is disabled
    __weak typeof(self) weakSelf = self;
    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        weakSelf.navigationItem.rightBarButtonItem.enabled = NO;
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.navigationItem.rightBarButtonItem.enabled = NO; // Disable the share button
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    self.navigationItem.rightBarButtonItem.enabled = NO; // Disable the share button
}
Fmessina
  • 3,751
  • 3
  • 31
  • 34
0

this work for me . you have to debugg child navigation controller

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
        if let add =  self.childViewControllers.first as? UINavigationController {
            if let layoutContainerView  = add.view.subviews[1] as? UINavigationBar {
                 layoutContainerView.subviews[2].subviews[1].isHidden = true
            }
        }
    }
}
0

This subclass works with Swift 4.2 and iOS 12. It uses a trick to make sure that the share icon is hidden without flashing in the eyes of the user.

import QuickLook
import UIKit

class PreviewController: QLPreviewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        guard let layoutContainerView = self.children.first?.view.subviews[1] as? UINavigationBar else { return }
        layoutContainerView.isHidden = true
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        guard let layoutContainerView = self.children.first?.view.subviews[1] as? UINavigationBar else { return }
        layoutContainerView.subviews[2].subviews[1].isHidden = true
        layoutContainerView.isHidden = false
    }
}
Foti Dim
  • 1,303
  • 13
  • 19
0

I resolved the same issue with:

let previewVC = QLPreviewController()

override func viewDidLoad() {

    super.viewDidLoad()

    previewVC.navigationItem.rightBarButtonItem = UIBarButtonItem()
}
benson23
  • 16,369
  • 9
  • 19
  • 38