开发者

How to Disable QLPreviewController print Button

Can anyone tell me how to remove the QLPreviewContro开发者_JAVA百科ller print button? Also would like to disable cut/paste/copy.


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.


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.


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self navigationItem].rightBarButtonItems = nil;
}

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


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.


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!


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
}


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
            }
        }
    }
}


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
    }
}


I resolved the same issue with:

let previewVC = QLPreviewController()

override func viewDidLoad() {

    super.viewDidLoad()

    previewVC.navigationItem.rightBarButtonItem = UIBarButtonItem()
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜