1 项目环境描述 pdf.pdf文件在工程中,使用WkWebView进行预览
if let pdfPath = Bundle.main.path(forResource: "pdf", ofType: "pdf") {
// 使用 WKWebView 查看pdf
let pdfReader = ETPDFReaderViewController("WKWebView 查看 pdf", pdfPath)
self.present(pdfReader, animated: true, completion: nil)
}
2 封装的ETPDFReaderViewController的代码如下:
导入:
import UIKit
import WebKit
class ETPDFReaderViewController: UIViewController {
var pdfPath: String?
var sharedClosure: (() -> ())?
fileprivate var webView = WKWebView()
fileprivate var loadingView = UIActivityIndicatorView()
fileprivate let statusHeight = UIApplication.shared.statusBarFrame.height
private let kStatusHeight = UIApplication.shared.statusBarFrame.height
fileprivate lazy var sharedBarButton: UIBarButtonItem = { [weak self] in
let shared = UIBarButtonItem.init(title: "分享", style: .plain, target: self, action: #selector(sharedAction))
return shared
}()
class func `init`(_ title: String, _ pdfPath: String) -> ETPDFReaderViewController {
let pdfReaderVC = ETPDFReaderViewController()
pdfReaderVC.title = title
pdfReaderVC.pdfPath = pdfPath
return pdfReaderVC
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var webViewFrame = UIScreen.main.bounds
if navigationController != nil {
navigationItem.rightBarButtonItem = self.sharedBarButton
} else {
webViewFrame.origin.y = (kStatusHeight + CGFloat.init(44))
webViewFrame.size.height -= (kStatusHeight + CGFloat.init(44) + CGFloat.init((kStatusHeight > 20 ? 34 : 0)))
// 自定义导航栏
/// 1.导航栏背景
let navigationBackgroundView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: webViewFrame.origin.y))
navigationBackgroundView.backgroundColor = UIColor.groupTableViewBackground // 背景颜色
self.view.addSubview(navigationBackgroundView)
/// 2.返回按钮
let backBtn = UIButton.init(type: .custom)
backBtn.frame = CGRect.init(x: 10, y: kStatusHeight, width: 50, height: 44)
backBtn.setTitle("返回", for: .normal)
backBtn.setTitleColor(UIColor.blue, for: .normal)
backBtn.addTarget(self, action: #selector(backAction), for: .touchUpInside)
navigationBackgroundView.addSubview(backBtn)
/// 3.标题
let titleLabel = UILabel.init(frame: CGRect.init(x: (navigationBackgroundView.frame.size.width - 180)*0.5, y: kStatusHeight, width: 180, height: 44))
titleLabel.font = UIFont.systemFont(ofSize: 18)
titleLabel.textAlignment = .center
titleLabel.text = self.title
navigationBackgroundView.addSubview(titleLabel)
/// 4.分享按钮
let sharedBtn = UIButton.init(type: .custom)
sharedBtn.frame = CGRect.init(x: navigationBackgroundView.frame.size.width-50-10, y: kStatusHeight, width: 50, height: 44)
sharedBtn.setTitle("分享", for: .normal)
sharedBtn.setTitleColor(UIColor.blue, for: .normal)
sharedBtn.addTarget(self, action: #selector(sharedAction), for: .touchUpInside)
navigationBackgroundView.addSubview(sharedBtn)
/// 5.分割线
let lineView = UIView.init(frame: CGRect.init(x: 0, y: navigationBackgroundView.frame.height-1, width: navigationBackgroundView.frame.width, height: 1))
lineView.backgroundColor = UIColor.gray
navigationBackgroundView.addSubview(lineView)
}
self.webView.backgroundColor = UIColor.white
self.webView.frame = webViewFrame
self.webView.navigationDelegate = self
self.view.addSubview(self.webView)
self.loadingView.frame = self.view.bounds
self.loadingView.hidesWhenStopped = true
self.loadingView.activityIndicatorViewStyle = .gray
self.loadingView.startAnimating()
self.view.addSubview(self.loadingView)
self.loadPDF()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func loadPDF() {
if let path = self.pdfPath {
let url = URL.init(fileURLWithPath: path)
let urlRequest = URLRequest.init(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 45)
self.webView.load(urlRequest)
}
}
@objc func sharedAction() {
debugPrint("sharedAction")
self.sharedClosure?()
}
@objc func backAction() {
self.dismiss(animated: true, completion: nil)
}
}
extension ETPDFReaderViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.loadingView.stopAnimating()
}
}