簡體   English   中英

如何繼承 QLPreviewController 並禁用共享按鈕?

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

  • 我已經完成了這樣的代碼,但它打開了 QLPreviewController,但沒有禁用共享按鈕。 我嘗試過不同的方法,但沒有用。

    - 或者您可以建議我任何不同的預覽 Controller,我可以在其中禁用共享按鈕。

  • qlViewController = QLPreviewController()

  • qlViewController.navigationItem.rightBarButtonItem = 無

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

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

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

測試於 iOS 15,可能會隨着下一次更新而改變。

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

有一個示例如何隱藏右側共享按鈕

final class AttachmentQuickLookVC: QLPreviewController {

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

此外,如果你想自定義后退按鈕,你可以這樣做:

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