'Why does PDFView show 1 page instead of 2 page in landscape or vice versa?

I am using PDFView with UIPageViewController.

While the app is in foreground and portrait mode it shows only 1 page on every page, which is correct.

But, when the app enters the background from the foreground and then again enters the foreground from the background it shows 2 pages instead of only 1 page in portrait mode.

The same issue happens for landscape mode. Also, it shows an empty page near of shown 1 page.

final class SpreadPDFView: PDFView {

  override var document: PDFDocument? {
    get { super.document }
    set {
      super.document = newValue
    }
  }

  var leftPageView: UIView? {
    guard
      let documentView = documentView,
      documentView.subviews.count >= 2,
      documentView.subviews[1].frame != .zero
    else { return nil }

    return documentView.subviews[1]
  }
  var rightPageView: UIView? {
    guard
      let documentView = documentView,
      documentView.subviews.count > 2,
      documentView.subviews[2].frame != .zero
    else { return nil }

    return documentView.subviews[2]
  }

  override init(frame: CGRect) {
    super.init(frame: frame)

    displayMode = .twoUp
//    displayMode = .singlePage It works properly when this value set to .singlePage but it show only 1 page on both .landscape and .portrait modes
//    displayMode = .twoUp It doesn't work properly when this value set to .twoUp on both .landscape and .portrait modes
//    displayMode = .singlePageContinuous It is not fit pafview in to screen needs scrolling
//    displayMode = .twoUpContinuous It behaves same like .twoUp mode
    displaysPageBreaks = true

    if Current.device.isPhone {
      pageBreakMargins = UIEdgeInsets(top: 8.0, left: 4.0, bottom: 8.0, right: 4.0)
    } else {
      pageBreakMargins = UIEdgeInsets(top: 4.0, left: 4.0, bottom: 4.0, right: 4.0)
    }
  }

  @available(*, unavailable)
  required init?(coder _: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override func layoutSubviews() {
    super.layoutSubviews()

    scaleToFitDocument()
  }

  // This disables text selection actions.
  override func canPerformAction(_: Selector, withSender _: Any?) -> Bool { false }

  override func layoutDocumentView() {
    super.layoutDocumentView()
    // This needs to be set to false to make it easier to pan with one finger.
    (subviews.first as? UIScrollView)?.isDirectionalLockEnabled = false
  }

  func scaleToFitDocument() {
    autoScales = true
    let scaleFactor = scaleFactorForSizeToFit
    guard scaleFactor > 0.0 else { return }

    minScaleFactor = scaleFactor
    self.scaleFactor = scaleFactor
  }
}

I'm using above PDFView in my ViewController like the following:

 class ViewController: UIViewController() {
      override func viewDidLoad() {
        super.viewDidLoad()
        setupPDFView()
      }

    private func setupPDFView() {
        view.addSubview(pdfView)
        pdfView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
          pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
          pdfView.topAnchor.constraint(equalTo: view.topAnchor),
          pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
          pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
      }
    }

And also an image for visualization:

image



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source