How to Disable QLPreviewController print Button

2020-02-10 05:21发布

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

8条回答
【Aperson】
2楼-- · 2020-02-10 05:49
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self navigationItem].rightBarButtonItems = nil;
}

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

查看更多
相关推荐>>
3楼-- · 2020-02-10 05:55

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
            }
        }
    }
}
查看更多
来,给爷笑一个
4楼-- · 2020-02-10 05:56

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.

查看更多
Evening l夕情丶
5楼-- · 2020-02-10 05:56

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.

查看更多
我只想做你的唯一
6楼-- · 2020-02-10 06:06

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.

查看更多
淡お忘
7楼-- · 2020-02-10 06:06

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
    }
}
查看更多
登录 后发表回答