SwiftUI 中的 QLPreviewController 缺少导航栏
Posted
技术标签:
【中文标题】SwiftUI 中的 QLPreviewController 缺少导航栏【英文标题】:QLPreviewController missing navigation bar in SwiftUI 【发布时间】:2020-04-22 05:28:39 【问题描述】:当我在工作表中呈现时,QLPreviewController 导航栏丢失。 它看起来像这样: 如何显示顶部导航栏或导航项?
QuickLookController UIKit 可在 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
【问题讨论】:
【参考方案1】:QuickLook preview 没有自己的导航控制器,我们有责任提供一个。
这是可能的方法。使用 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
【讨论】:
以上是关于SwiftUI 中的 QLPreviewController 缺少导航栏的主要内容,如果未能解决你的问题,请参考以下文章