gpt4 book ai didi

ios - 在选择打印机之前获取空白 View

转载 作者:行者123 更新时间:2023-12-01 21:30:59 25 4
gpt4 key购买 nike

当我调用以下代码时,在获得打印机选择 View 之前,我会看到一个空白 View 。一旦我关闭(滑动)空白 View ,之后一切似乎都正常。调用代码如下所示。我在另一个 View 中定义了一个 NavigationView,该 View 导致了进行此调用的 View 。有什么建议么?

struct PrintHTMLView: UIViewControllerRepresentable
{
var htmlString: String
var title: String

typealias UIViewControllerType = UIViewController

fileprivate let controller = UIViewController()

func makeUIViewController(context: Context) -> UIViewController
{
controller
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context)
{
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = title
printInfo.outputType = .general

let printController = UIPrintInteractionController.shared
printController.printInfo = printInfo
printController.showsNumberOfCopies = true

let formatter = UIMarkupTextPrintFormatter(markupText: htmlString)
formatter.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
printController.printFormatter = formatter
printController.present(animated: true, completionHandler: nil)
}
}
调用上述代码的代码片段......
Button("Print")
{
self.printShowing.toggle()
}.sheet(isPresented: $printShowing)
{
PrintHTMLView(htmlString: html, title: "Report")
}

最佳答案

这是固定的变体。重点是UIPrintInteractionController显示自己的工作表,因此不需要它,updateUIViewController目的不同 - 更新外部数据变化的内容,所以所有的准备都应该在makeUIViewController .
注:UIMarkupTextPrintFormatter引用应保留在属性中,否则它会丢失并导致打印表关闭时崩溃。
使用 Xcode 12/iOS 14 测试
demo

struct PrintHTMLView: UIViewControllerRepresentable
{
let title: String
let formatter: UIMarkupTextPrintFormatter
let callback: () -> ()

init(htmlString: String, title: String, completion: @escaping () -> () = {}) {
self.title = title
self.callback = completion

formatter = UIMarkupTextPrintFormatter(markupText: htmlString)
formatter.perPageContentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
}

func makeUIViewController(context: Context) -> UIViewController
{
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = title
printInfo.outputType = .general

let printController = UIPrintInteractionController.shared
printController.printInfo = printInfo
printController.showsNumberOfCopies = true

printController.printFormatter = formatter

let controller = UIViewController()
DispatchQueue.main.async {
printController.present(animated: true, completionHandler: { _, _, _ in
printController.printFormatter = nil
self.callback()
})
}
return controller
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context)
{
}
}

struct TestPrintController: View {
@State private var printShowing = false
var body: some View {
Button("Print")
{
self.printShowing = true
}
.background(Group {
if self.printShowing {
PrintHTMLView(htmlString: "Test String", title: "Report") {
self.printShowing = false
}
}
})

}
}

关于ios - 在选择打印机之前获取空白 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62999655/

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