简体   繁体   English

如何禁用 QLPreviewController 打印按钮

[英]How to Disable QLPreviewController print Button

Can anyone tell me how to remove the QLPreviewController print button?谁能告诉我如何删除 QLPreviewController 打印按钮? Also would like to disable cut/paste/copy.还想禁用剪切/粘贴/复制。

UPDATE: 更新:

This no longer works in iOS 6. Quick Look runs in another process using XPC. 这在iOS 6中不再有效。快速查看在使用XPC的另一个进程中运行。 See [here][3] for more details. 有关详细信息,请参见[此处] [3]。 I don't foresee any way to customize QLPreviewController. 我没有预见到任何定制QLPreviewController的方法。 The following answer remains for anyone interested for pre-iOS 6. 以下答案适用于对iOS 6之前感兴趣的任何人。


If you want to simply remove the action button you need to subclass QLPreviewController . 如果您只想删除操作按钮,则需要QLPreviewController Then in -viewWillAppear: you need to remove the action button by calling [[self navigationItem] setRightBarButtonItem:nil]; 然后在-viewWillAppear:你需要通过调用[[self navigationItem] setRightBarButtonItem:nil];来删除动作按钮[[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. 需要注意的一点是, QLPreviewController不是为了自定义。 You can check out this repository on Github. 您可以在Github上查看此存储库 It contains a QLPreviewController that is already safely subclassed. 它包含一个已经安全子类化的QLPreviewController。 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 . 更好但更复杂的解决方案是使用UIDocumentInteractionController From what I understand of QLPreviewController is that it's built using UIDocumentInteractionController . 根据我对QLPreviewController理解,它是使用UIDocumentInteractionController QLPreviewController was made for general use and easy addition. QLPreviewController是为一般使用而且易于添加的。 UIDocumentInteractionController gives much more control, but is harder to use. UIDocumentInteractionController提供了更多的控制,但更UIDocumentInteractionController I can't sufficiently describe how to use it here. 我不能在这里充分描述如何使用它。 I recommend checking out WWDC 2010 session 106 Understanding Document Interaction Controller. 我建议您查看WWDC 2010会话106了解文档交互控制器。

As for disabling cut/copy/paste, you can't do that with QLPreviewController . 至于禁用剪切/复制/粘贴,你不能用QLPreviewController做到这QLPreviewController You might be able to do that with UIDocumentInteractionController , but I wouldn't count on it. 您可以使用UIDocumentInteractionController执行此UIDocumentInteractionController ,但我不会指望它。 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. 为纯文本,照片,视频和PDF制作观众相对容易。 Office documents are more effort than it's worth. Office文档比它的价值更多。

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. 我已经建立了将动作按钮移到RBFilePreviewer因此您不必担心自己动手。

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

Works for me instead of [[self navigationItem] setRightBarButtonItem:nil]; 适用于我而不是[[self navigationItem] setRightBarButtonItem:nil];

If you subclass QLPreviewController and then add this one method: 如果你QLPreviewController ,然后添加这个方法:

-(void)viewDidAppear:(BOOL)animated{

    [[self navigationItem] setRightBarButtonItem:nil]; 
}

in the subclass, the action button will disappear as rbrown notes above. 在子类中,动作按钮将消失,如上面的rbrown说明。 This will not work if you use viewWillAppear . 如果使用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. 此外,执行此操作的意外副作用是预览中的工具栏现在始终显示,而不是仅在您点击视图时。

I also need to custom the navigaiton item of QLPreviewController . 我还需要定制的导览树状项目QLPreviewController Just like rbrown said, XPC is used and we can no longer add custom items in viewDidLoad or viewWillAppear . 就像rbrown说的那样,使用了XPC,我们无法再在viewDidLoadviewWillAppear添加自定义项。

Currently I use this way: 目前我用这种方式:

  1. create a new class of UIViewController 创建一个新的UIViewController
  2. add the view of QLPreviewController to the new class QLPreviewController的视图添加到新类

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! 如果我没有在UIView中包含previewController,那么它将无法工作!

I have found a solution to disable the rightBarButtonItem in QLPreviewController that works fine for me in iOS8 and iOS9 我找到了一个解决方案来禁用QLPreviewController中的rightBarButtonItem ,它在iOS8和iOS9中对我来说很好用

You simply need to subclass QLPreviewController and override the following methods, then use your subclass instead of the original QLPreviewController 您只需要子类化QLPreviewController并覆盖以下方法,然后使用您的子类而不是原始的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 . 此子类适用于Swift 4.2iOS 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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM