objective-ciphoneswiftuiprintinteractioncntrleruiprintpagerenderer

UIPrintInteractionController appends a blank page when the orientation is set to Landscape


I'm trying to send a string to the printer using UIMarkupTextPrintFormatter and everything is allright when I don't change the page orientation -which is set to portrait by default.

Portrait print preview

However, when I set the page orientation to landscape, one blank page is appended to the rendered output, as if the margins are beyond of the first page.

Landscape print preview

Here's my code for printing:

let printController = UIPrintInteractionController.sharedPrintController()

let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = "Print Job"
printInfo.orientation = .Landscape
printController.printInfo = printInfo

let formatter = UIMarkupTextPrintFormatter(markupText: "Welcome")
formatter.contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
printController.printFormatter = formatter

printController.presentAnimated(true, completionHandler: nil)

Another thing to note is that when the page is set to landscape, Xcode logs some errors related to autolayout.

the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa066097850>, and it is attached to <UICollectionView: 0x7fa06687f800; frame = (0 0; 375 313.5); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa066095e90>; layer = <CALayer: 0x7fa066098b30>; contentOffset: {0, 0}; contentSize: {666, 313.5}> collection view layout: <UICollectionViewFlowLayout: 0x7fa066097850>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa066097850>, and it is attached to <UICollectionView: 0x7fa06687f800; frame = (0 0; 375 313.5); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa066095e90>; layer = <CALayer: 0x7fa066098b30>; contentOffset: {0, 0}; contentSize: {666, 313.5}> collection view layout: <UICollectionViewFlowLayout: 0x7fa066097850>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
Unbalanced calls to begin/end appearance transitions for <UIViewController: 0x7fa066085830>.
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa063c55130>, and it is attached to <UICollectionView: 0x7fa064043000; frame = (0 0; 375 313.5); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa063c4a720>; layer = <CALayer: 0x7fa063c26e60>; contentOffset: {0, 0}; contentSize: {666, 313.5}> collection view layout: <UICollectionViewFlowLayout: 0x7fa063c55130>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa063c55130>, and it is attached to <UICollectionView: 0x7fa064043000; frame = (0 0; 375 313.5); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa063c4a720>; layer = <CALayer: 0x7fa063c26e60>; contentOffset: {0, 0}; contentSize: {666, 313.5}> collection view layout: <UICollectionViewFlowLayout: 0x7fa063c55130>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

I have tried to print only first page but couldn't find anything about setting a page range, UIMarkupTextPrintFormatter has a startPage variable can be set, but setting it is no luck, even when I set it to the second page, it renders like it wasn't set to anything.

I can either solve this problem by getting rid of the blank page or setting the print range to page 1 only, but so far I couldn't accomplish any of them. Any ideas?


Solution

  • It appears that I can work this around not by setting the printFormatter for UIPrintInteractionController directly, but creating a UIPrintPageRenderer and then appending my formatter to the renderer before setting it to the UIPrintInteractionController.

    Here's the solution:

    // printController.printFormatter = formatter // cancelled
    let printRenderer = UIPrintPageRenderer()
    printRenderer.addPrintFormatter(formatter, startingAtPageAtIndex: 0)
    printController.printPageRenderer = printRenderer