3

I am trying to implement pencil kit toolkit (a bar of pencil, eraser, etc.) that appears at the bottom of the screen. However, upon running this line of code:

guard let window = view.window, let toolPicker = PKToolPicker.shared(for: window)
else {return}

I get the following error in the log and the toolpicker does not appear:

    PDF Reader[926:85385] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60000191d450 UIView:0x7fdfb7376ff0.height == 75   (active)>",
    "<NSLayoutConstraint:0x60000191d4a0 V:|-(0)-[UIView:0x7fdfb7376ff0]   (active, names: '|':PKPaletteContainerView:0x7fdfb737b7e0 )>",
    "<NSLayoutConstraint:0x60000191dae0 V:|-(0)-[PKPaletteContainerView:0x7fdfb737b7e0]   (active, names: '|':UIView:0x7fdfb46181d0 )>",
    "<NSLayoutConstraint:0x60000191dc70 PKPaletteContainerView:0x7fdfb737b7e0.bottom == UIView:0x7fdfb46181d0.bottom   (active)>",
    "<NSLayoutConstraint:0x600001901ea0 V:|-(0)-[UIView:0x7fdfb46181d0]   (active, names: '|':PKPaletteView:0x7fdfb462d3e0 )>",
    "<NSLayoutConstraint:0x600001901ef0 UIView:0x7fdfb46181d0.bottom == PKPaletteView:0x7fdfb462d3e0.bottom   (active)>",
    "<NSLayoutConstraint:0x6000019fcf00 PKPaletteView:0x7fdfb462d3e0.height == 122   (active)>",
    "<NSLayoutConstraint:0x60000191d720 UIView:0x7fdfb7376ff0.bottom == PKPaletteContainerView:0x7fdfb737b7e0.bottom   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000191d450 UIView:0x7fdfb7376ff0.height == 75   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

I'm not really sure how to mess around with the constraints of the PKPalette or what constraints are conflicting below. I've also tried disabling translatesAutoResizingMaskIntoConstraints and it doesn't work as other parts of my application rely on it. Any help is appreciated, thank you!

user65909
  • 101
  • 1
  • 9
  • Try to identify the unwanted constraint and remove it. You can use View Hierarchy Debugger to check If you are using storyboard evaluate the constraints you added for your component. [this reference can help you](https://stackoverflow.com/questions/17803801/unable-to-simultaneously-satisfy-constraints) – Dinesh Nagarajan Jan 29 '20 at 06:40

3 Answers3

1

I get the same log messages when calling PKToolPicker on iPhone. On iPad, I do no get the constraint errors. If I look through the constraint conflicts, it all seems internal to the PKToolPicker and nothing to do with my layout setup.

If you run the same code, but with the iPhone in Landscape rather than Portrait, the conflicts do not occur. I therefore suspect that the PKPickerTool constraints have some sort of limitation that Apple needs to fix when the width of the screen is too narrow. I'll post a radar.

Zatman
  • 144
  • 2
0

It's not because of the constraints; you need to call these lines:

toolPicker.setVisible(true, forFirstResponder: canvasView)
canvasView.becomeFirstResponder()

You can find more info here:

https://developer.apple.com/documentation/pencilkit/drawing_with_pencilkit https://developer.apple.com/videos/play/wwdc2019/221/

0

I have the same situation. If you set the layout breakpoint, I can see the views involved are all in Apple's own, and they even exist in a private overlay window, not the main window. So seems a legit bug in the framework.

I found that for me it would fail the first time I tried to get the tools. To workaround, I now simply get the tools once very early on after launch. I don't use them, but this triggers the bug, so that when I really need the tools, they work.

if #available(iOS 13.0, *) {
    PKToolPicker.shared(for: view.window!)
}
Drew McCormack
  • 3,490
  • 1
  • 19
  • 23