Reputation: 290
QLPreviewController navigation bar missing when I presented in sheet.
It looks like this :
How can I show up top navigation bar or navigationItem?
QuickLookController UIKit representable in swiftUI
.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
}
}
}
Upvotes: 3
Views: 2087
Reputation: 3303
Here is an implementation that can handle multiple urls:
import SwiftUI
import QuickLook
struct QuickLookView: UIViewControllerRepresentable {
var urls: [URL]
func updateUIViewController(_ uiViewController: QLPreviewController,
context: UIViewControllerRepresentableContext<QuickLookView>) {
uiViewController.reloadData()
}
func makeUIViewController(context: Context) -> QLPreviewController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
controller.reloadData()
return controller
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, QLPreviewControllerDataSource {
var parent: QuickLookView
init(_ qlPreviewController: QuickLookView) {
self.parent = qlPreviewController
super.init()
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return parent.urls.count
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return parent.urls[index] as QLPreviewItem
}
}
}
Then wrap the QuickLookView in a container and access that from your views:
struct QuickLookContainerView: View {
let urls: [URL]
var body: some View {
NavigationView {
QuickLookView(urls: urls)
}
}
}
Upvotes: 1
Reputation: 258117
QuickLook preview does not have own navigation controller, it is our responsibility to provided one.
Here is possible approach. Tested with 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
}
}
}
Upvotes: 11