gpt4 book ai didi

swift - QLPreviewController 在 SwiftUI 中缺少导航栏

转载 作者:行者123 更新时间:2023-12-05 08:50:47 26 4
gpt4 key购买 nike

当我在工作表中显示时,缺少 QLPreviewController 导航栏。看起来像这样:Image如何显示顶部导航栏或 navigationItem?

可在 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 预览没有自己的导航 Controller ,我们有责任提供一个。

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

demo

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
}

}
}

关于swift - QLPreviewController 在 SwiftUI 中缺少导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61357888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com