QLPreviewController 在 SwiftUI 中缺少导航栏

QLPreviewController missing navigation bar in SwiftUI

我在 sheet 中呈现时缺少 QLPreviewController 导航栏。 看起来像这样: 如何显示顶部导航栏或导航项?

可在 swiftUI 中表示的 QuickLookController UIKit

.sheet(isPresented: $showQuickLook, onDismiss: {self.showQuickLook = false}) {
                        if  self.selectedURL != nil {
                            QuickLookController(url: self.selectedURL!) {
                                self.showQuickLook = false
                            }.edgesIgnoringSafeArea(.all)
                        }
                    }


struct QuickLookController: UIViewControllerRepresentable {

    var url: URL
    var onDismiss: () -> Void

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func updateUIViewController(_ uiViewController: QLPreviewController, context: UIViewControllerRepresentableContext<QuickLookController>) {
        uiViewController.reloadData()
    }

    func makeUIViewController(context: Context) -> QLPreviewController {
        let controller = QLPreviewController()

        controller.dataSource = context.coordinator
        controller.reloadData()
        return controller
    }

    class Coordinator: NSObject, QLPreviewControllerDataSource {
        var parent: QuickLookController

        init(_ qlPreviewController: QuickLookController) {
            self.parent = qlPreviewController
            super.init()
        }
        func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
            return 1
        }
        func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
            return self.parent.url as QLPreviewItem
        }

    }
}

QuickLook 预览版没有自己的导航控制器,我们有责任提供一个。

这是可能的方法。使用 Xcode 11.4 / iOS 13.4

测试

struct TestQLPreviewController: View {
    @State private var showQuickLook = false

    // just for demo - document.pdf is located in main bundle
    @State private var selectedURL = Bundle.main.url(forResource: "document", withExtension: "pdf")
    var body: some View {
        Button("Show") { self.showQuickLook.toggle() }
            .sheet(isPresented: $showQuickLook, onDismiss: {self.showQuickLook = false}) {
                                    if  self.selectedURL != nil {
                                        QuickLookController(url: self.selectedURL!) {
                                            self.showQuickLook = false
                                        }
                                    }
                                }
    }
}


struct QuickLookController: UIViewControllerRepresentable {

    var url: URL
    var onDismiss: () -> Void

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func updateUIViewController(_ viewController: UINavigationController, context: UIViewControllerRepresentableContext<QuickLookController>) {
        if let controller = viewController.topViewController as? QLPreviewController {
            controller.reloadData()
        }
    }

    func makeUIViewController(context: Context) -> UINavigationController {
        let controller = QLPreviewController()

        controller.dataSource = context.coordinator
        controller.reloadData()
        return UINavigationController(rootViewController: controller)
    }

    class Coordinator: NSObject, QLPreviewControllerDataSource {
        var parent: QuickLookController

        init(_ qlPreviewController: QuickLookController) {
            self.parent = qlPreviewController
            super.init()
        }
        func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
            return 1
        }
        func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
            return self.parent.url as QLPreviewItem
        }

    }
}

backup