You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中如何在PDF显示页面添加并正常使用返回按钮?

问题分析与修复方案

核心问题

  1. 误用PDFView的goBack方法:你调用的pdfView.goBack(navigationController?.popViewController(animated: true))完全错误,这个方法是用于PDF文档内部的页面导航(比如跳转到之前浏览过的PDF页面),和返回上一个ViewController毫无关系,属于无效代码。
  2. PDFView遮挡自定义返回按钮:你创建的PDFView使用view.frame作为布局范围,会铺满整个视图,直接覆盖了通过IBOutlet添加的backButton,导致按钮无法被点击触发事件。
  3. 可选优化点:使用系统导航栏自带的返回按钮比自定义按钮更可靠,无需额外添加IBOutlet和布局适配。

修复后的代码

方案一:使用系统导航栏返回按钮(推荐)

这种方式无需自定义按钮,直接利用UINavigationController的默认返回按钮,代码更简洁稳定:

import Foundation
import PDFKit

class ClaimProcedure : UIViewController{
    let claimPdf = "claim_process"

    override func viewDidLoad() {
       super.viewDidLoad()
       title = "理赔流程" // 设置导航栏标题后,系统会自动生成返回按钮
       
       let pdfView = PDFView()
       pdfView.translatesAutoresizingMaskIntoConstraints = false
       view.addSubview(pdfView)
       
       // 设置PDFView约束,适配安全区域,避免覆盖导航栏
       NSLayoutConstraint.activate([
           pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
           pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
           pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
           pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
       ])
       
       if let url = Bundle.main.url(forResource: claimPdf, withExtension: "pdf"),
          let pdfDocument = PDFDocument(url: url) {
           pdfView.displayMode = .singlePageContinuous
           pdfView.autoScales = true
           pdfView.displayDirection = .vertical
           pdfView.document = pdfDocument
       }
    }
}

方案二:保留自定义返回按钮并修复遮挡问题

如果你坚持使用自定义按钮,需要调整PDFView的布局范围,避免遮挡按钮:

import Foundation
import PDFKit

class ClaimProcedure : UIViewController{
    @IBOutlet weak var backButton: UIButton!
    let claimPdf = "claim_process"

    override func viewDidLoad() {
       super.viewDidLoad()
       title = "理赔流程"
       
       let pdfView = PDFView()
       pdfView.translatesAutoresizingMaskIntoConstraints = false
       view.addSubview(pdfView)
       
       // 设置约束,让PDFView在自定义返回按钮下方显示(可根据你的按钮位置调整间距)
       NSLayoutConstraint.activate([
           pdfView.topAnchor.constraint(equalTo: backButton.bottomAnchor, constant: 16),
           pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
           pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
           pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
       ])
       
       if let url = Bundle.main.url(forResource: claimPdf, withExtension: "pdf"),
          let pdfDocument = PDFDocument(url: url) {
           pdfView.displayMode = .singlePageContinuous
           pdfView.autoScales = true
           pdfView.displayDirection = .vertical
           pdfView.document = pdfDocument
       }
    }

    @IBAction func back(_ sender: Any) {
        // 增加容错判断,确保导航控制器存在
        if let navController = navigationController {
            navController.popViewController(animated: true)
        } else {
            // 万一当前不在导航栈中,用dismiss作为降级方案
            dismiss(animated: true)
        }
    }
}

额外检查项

  • 确认原ViewController确实处于UINavigationController的栈中,否则pushViewController和popViewController都无法生效。
  • 检查自定义backButton的IBOutlet是否正确连接,没有出现断连情况。
  • 确保claim_process.pdf文件已添加到项目中,且勾选了对应的Target Membership。

内容的提问来源于stack exchange,提问作者Simran Bhavsar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 04:40:24