I created a new macOS document-based app in Xcode Version 11.5 (11E608c), and modified ContentView to look like this: struct ContentView: View { var body: some View { ZStack { Circle() .foregroundColor(.red) .frame(width: 30, height: 30, alignment: .center) Text("Hello, World!") } .frame(maxWidth: .infinity, maxHeight: .infinity) } } I made no other changes other than to enable the print capability. The canvas and app itself look just as you'd expect. When I run File->Print, however, only the window, its frame, and the text show up in the print preview and on the printed page. The circle graphic is missing, even though the appropriate light/dark mode background is there. Is this a bug, or is there something else required to enable graphics to print from SwiftUI?
printing with swiftui
Apologies for the lack of formatting. I expected leading spaces would do it.
That will look better:
struct ContentView: View {
var body: some View {
ZStack {
Circle()
.foregroundColor(.red)
.frame(width: 30, height: 30, alignment: .center)
Text("Hello, World!")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
I tried the same. And got the same problem…
That looks like a limit of built in print command.
You probably have to do as in MacOS to print in a graphic port ?
Tutorials are silent about it …
https://developer.apple.com/tutorials/swiftui/creating-a-macos-app
Did you find a solution to this issue?
I have exhausted my ability to Google over the last couple of days trying to solve the issue.
In my case I have a view with a background Image + some text. The text prints fine on a nice clean background - no image!
Print code:
Code Block language @IBAction func print2(_ sender : Any){ let printingView = self.window.contentView let printInfo = NSPrintInfo() let printOperation = NSPrintOperation(view: printingView!, printInfo: printInfo) printOperation.showsPrintPanel = true printOperation.showsProgressPanel = true printOperation.run() }
I am using latest version of Xcode Swift and SwiftUi
I've seen Paul Hudson's sample on simple printing, (https://www.hackingwithswift.com/example-code/uikit/how-to-print-using-uiactivityviewcontroller). I'm still looking for how to present a system share sheet, which should allow me to "export" data to whatever form a share action wants, be it printing, tweeting, emailing, and so on.
(Apple forums doesn't like me to post live URLs to that site: This domain "https://www.hackingwithswift.com/example-code/uikit/how-to-print-using-uiactivityviewcontroller" is not a permitted domain on this forums.)
No sooner do I post this than I finally find an example that looks like it'll work (https://jeevatamil.medium.com/how-to-create-share-sheet-uiactivityviewcontroller-in-swiftui-cef64b26f073). I'll try to remember to post working code that connects this to printing.