当方向设置为横向时,UIPrintInteractionController 附加一个空白页

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

我正在尝试使用 UIMarkupTextPrintFormatter 向打印机发送一个字符串,当我不更改页面方向时一切正常 - 默认设置为纵向。

但是,当我将页面方向设置为横向时,一个空白页面会附加到呈现的输出中,就好像页边距超出了第一页一样。

这是我的打印代码:

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)

另一件需要注意的事情是,当页面设置为横向时,Xcode 会记录一些与自动布局相关的错误。

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.

我试过只打印第一页,但找不到关于设置页面范围的任何信息,UIMarkupTextPrintFormatter 有一个 startPage 变量可以设置,但设置它并不成功,即使当我将它设置到第二页时,它呈现的就像没有设置任何东西一样。

我可以通过删除空白页或将打印范围设置为仅第 1 页来解决这个问题,但到目前为止我无法完成任何一个。有什么想法吗?

看来我不能通过直接为 UIPrintInteractionController 设置 printFormatter 来解决这个问题,而是创建一个 UIPrintPageRenderer 然后将我的格式化程序附加到渲染器,然后再将其设置为UIPrintInteractionController

解决方法如下:

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