I had undertaking going nice, the place i wanted to do stuff with pdfs, drawing on prime them and so on. Since apple is all closed sourced i wanted to turn into a bit hacky. In any case, i’ve an issue for the reason that new ios 26 replace which breaks the behaviour. I simplified the code very a lot right into a demo undertaking, the place you’ll be able to shortly see what’s incorrect.
When swiping left to go to the subsequent web page, it does change the web page and so on within the pdf Doc, however visually nothing occurs. I’m caught on the primary web page. I dont know what to do, tried loads of issues, however nothing works. Anybody expert sufficient to assist me out?
import UIKit
import PDFKit
import SwiftUI
class PDFViewController: UIViewController {
var pdfView: PDFView!
var gestureHandler: GestureHandler!
override func viewDidLoad() {
tremendous.viewDidLoad()
setupPDFView()
setupGestureHandler()
loadPDF()
}
personal func setupPDFView() {
pdfView = PDFView(body: view.bounds)
// Your actual configuration
pdfView.autoScales = true
pdfView.pageShadowsEnabled = false
pdfView.backgroundColor = .white
pdfView.displayMode = .singlePage
view.addSubview(pdfView)
// Setup constraints
pdfView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
pdfView.topAnchor.constraint(equalTo: view.topAnchor),
pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
personal func setupGestureHandler() {
gestureHandler = GestureHandler(pdfView: pdfView)
gestureHandler.setupSwipeGestures(on: view)
}
personal func loadPDF() {
if let path = Bundle.foremost.path(forResource: "sonate12", ofType: "pdf"),
let doc = PDFDocument(url: URL(fileURLWithPath: path)) {
pdfView.doc = doc
} else {
print("Couldn't discover sonate12.pdf in bundle")
}
}
}
class GestureHandler {
personal weak var pdfView: PDFView?
init(pdfView: PDFView) {
self.pdfView = pdfView
}
func setupSwipeGestures(on view: UIView) {
// Left swipe - go to subsequent web page
let leftSwipe = UISwipeGestureRecognizer(goal: self, motion: #selector(handleSwipe(_:)))
leftSwipe.route = .left
view.addGestureRecognizer(leftSwipe)
// Proper swipe - go to earlier web page
let rightSwipe = UISwipeGestureRecognizer(goal: self, motion: #selector(handleSwipe(_:)))
rightSwipe.route = .proper
view.addGestureRecognizer(rightSwipe)
}
@objc personal func handleSwipe(_ gesture: UISwipeGestureRecognizer) {
guard let pdfView = pdfView,
let doc = pdfView.doc,
let currentPage = pdfView.currentPage else {
print("🚫 No PDF view, doc, or present web page accessible")
return
}
let currentIndex = doc.index(for: currentPage)
let totalPages = doc.pageCount
print("📄 Present state: Web page (currentIndex + 1) of (totalPages)")
print("👆 Swipe route: (gesture.route == .left ? "LEFT (subsequent)" : "RIGHT (earlier)")")
swap gesture.route {
case .left:
// Subsequent web page
guard currentIndex 0 else {
print("🚫 Already on first web page (1), can not return")
return
}
let previousPage = doc.web page(at: currentIndex - 1)
if let web page = previousPage {
print("⬅️ Going to web page (currentIndex)")
pdfView.go(to: web page)
pdfView.setNeedsDisplay()
pdfView.layoutIfNeeded()
let bounds = pdfView.bounds
pdfView.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.width + 0.01, peak: bounds.peak)
pdfView.bounds = bounds
// Examine if navigation really labored
DispatchQueue.foremost.asyncAfter(deadline: .now() + 0.1) {
if let newCurrentPage = pdfView.currentPage {
let newIndex = doc.index(for: newCurrentPage)
print("✅ Navigation end result: Now on web page (newIndex + 1)")
if newIndex == currentIndex {
print("⚠️ WARNING: Web page did not change visually!")
}
}
}
} else {
print("🚫 Couldn't get earlier web page object")
}
default:
print("🤷♂️ Unknown swipe route")
break
}
}
}
struct PDFViewerRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> PDFViewController {
return PDFViewController()
}
func updateUIViewController(_ uiViewController: PDFViewController, context: Context) {
// No updates wanted
}
}
You’ll be able to take a look at the code right here as properly: https://github.com/vallezw/swift-bug-ios26