简体   繁体   English

如何继承 QLPreviewController 并禁用共享按钮?

[英]How to Subclass QLPreviewController and Disable Share button?

  • I have done code like this but it's open QLPreviewController but Share button was not disabled.我已经完成了这样的代码,但它打开了 QLPreviewController,但没有禁用共享按钮。 I have tried different things but it's not worked.我尝试过不同的方法,但没有用。

    - Or You Can Suggest me any different Preview Controller in which i can Disable Share button. - 或者您可以建议我任何不同的预览 Controller,我可以在其中禁用共享按钮。

  • qlViewController = QLPreviewController() qlViewController = QLPreviewController()

  • qlViewController.navigationItem.rightBarButtonItem = nil qlViewController.navigationItem.rightBarButtonItem = 无

  • qlViewController.delegate = self qlViewController.delegate = self
  • qlViewController.dataSource = self qlViewController.dataSource = self

func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int { return 1 } func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int { return 1 }

func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem {
    controller.navigationItem.rightBarButtonItem = nil
    return fileUrlToOpen
}

Tested on iOS 15, may change with next updates.测试于 iOS 15,可能会随着下一次更新而改变。

QLPreviewController now embeds a UINavigationController which then has a root controller with a standard navigationItem that we can tweak. QLPreviewController现在嵌入一个UINavigationController ,然后它有一个根 controller 和一个我们可以调整的标准navigationItem

guard let navigationItem = (aQLPreviewController.children.first as? UINavigationController)?.viewControllers.first?.navigationItem else
{
    // Not iOS 15 or the QLPreviewController implementation has changed
    return
}

// Do whatever you want with the navigationItem
navigationItem.rightBarButtonItem?.isEnabled = false
// or navigationItem.rightBarButtonItem = nil

There is an example how to hide right share button 有一个示例如何隐藏右侧共享按钮

final class AttachmentQuickLookVC: QLPreviewController {

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        self.navigationItem.rightBarButtonItems = [UIBarButtonItem]()        
    }
}

Also, u can do something like this if u want to customize back button: 此外,如果你想自定义后退按钮,你可以这样做:

final class AttachmentQuickLookVC: QLPreviewController {

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()        
        let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 35))

        backButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        backButton.contentHorizontalAlignment = .left
        backButton.setImage(UIImage(named: "ic_back"), for: .normal)

        let barButton = UIBarButtonItem(customView: backButton)
        self.navigationItem.leftBarButtonItems = [barButton]
        self.navigationItem.hidesBackButton = true
    }
}

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

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