11 Replies
      Latest reply on Mar 26, 2020 1:01 AM by Claude31
      druvinskiy Level 1 Level 1 (0 points)

        I am trying to create an app using PencilKit. I have the following code in one of my ViewControllers.

         

        ```

        override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)

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

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

        ```

         

        Although I am calling the setVisible function and making the canvasView the firstResponder, my toolPicker is not showing up, and printing toolPicker.isVisible is false.

        • Re: PencilKit toolPicker is not showing up
          Claude31 Level 8 Level 8 (8,505 points)

          Where and how do you define canvasView ?

           

          Did you add it to view ?

           

          I think you should have:

           

          override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
          
              let canvasView = PKCanvasView(frame: self.view.bounds)
            guard
            let window = view.window,
            let toolPicker = PKToolPicker.shared(for: window) else { return }
          
            toolPicker.setVisible(true, forFirstResponder: canvasView)
            toolPicker.addObserver(canvasView)
            canvasView.becomeFirstResponder()
            view.addSubview(canvasView)
          }
            • Re: PencilKit toolPicker is not showing up
              druvinskiy Level 1 Level 1 (0 points)

              canvasView is initalized in viewDidLoad:

               

              canvasView = PKCanvasView(frame: view.bounds)

                • Re: PencilKit toolPicker is not showing up
                  Claude31 Level 8 Level 8 (8,505 points)

                  And did you add it as subview ?

                    • Re: PencilKit toolPicker is not showing up
                      druvinskiy Level 1 Level 1 (0 points)

                      Yep, I just copied and pasted your version of the code, and it's still not showing up. I can draw on the canvas. It's just there's no toolPicker.

                        • Re: PencilKit toolPicker is not showing up
                          Claude31 Level 8 Level 8 (8,505 points)

                          I tested on simulator, this exact code:

                           

                          import PencilKit
                          
                          
                               override func viewDidAppear(_ animated: Bool) {
                                   
                                    super.viewDidAppear(animated) // Manquait
                                    setTempData()
                                    if #available(iOS 13.0, *) {
                                         let canvasView = PKCanvasView(frame: self.view.bounds)
                                         guard
                                         let window = view.window,
                                         let toolPicker = PKToolPicker.shared(for: window) else { return }
                                        
                                         toolPicker.setVisible(true, forFirstResponder: canvasView)
                                         toolPicker.addObserver(canvasView)
                                         canvasView.becomeFirstResponder()
                                         view.addSubview(canvasView)
                                    } else {
                                         // Fallback on earlier versions
                                    }
                               }

                           

                          works "well": I mean I see the pens at the bottom.

                           

                          Added

                                         print(toolPicker.isVisible)

                           

                          and get true in log

                            • Re: PencilKit toolPicker is not showing up
                              druvinskiy Level 1 Level 1 (0 points)

                              I just copied and pasted this exact code aside from the setTempData, and I don't see the pens at the bottom.

                                • Re: PencilKit toolPicker is not showing up
                                  Claude31 Level 8 Level 8 (8,505 points)

                                  Yes, I really copied so setTempData()  was superfluous !

                                   

                                  Do you run on device or simulator ?

                                   

                                  Which iOS version (iOS 13 required)

                                    • Re: PencilKit toolPicker is not showing up
                                      druvinskiy Level 1 Level 1 (0 points)

                                      I'm running it on a simulator. It is iOS 13.

                                        • Re: PencilKit toolPicker is not showing up
                                          Claude31 Level 8 Level 8 (8,505 points)

                                          If you want, you can post an email address for a minute, so that we can share files.

                                            • Re: PencilKit toolPicker is not showing up
                                              druvinskiy Level 1 Level 1 (0 points)

                                              I don't really feel comfortable giving my email address, but the project should  be accessible on my Github: https://github.com/druvinskiy/InstaKids under the Firebrase branch.

                                                • Re: PencilKit toolPicker is not showing up
                                                  Claude31 Level 8 Level 8 (8,505 points)

                                                  I could not run your code, I miss JGProgressHUD.

                                                   

                                                  But I noticed your code is not exactly the same I proposed:

                                                   

                                                  you have:

                                                   

                                                      override func viewDidLoad() {
                                                          super.viewDidLoad()
                                                         
                                                          setNavigationBar()
                                                         
                                                          let canvasView = PKCanvasView(frame: view.bounds)
                                                          self.canvasView = canvasView
                                                          canvasView.allowsFingerDrawing = true
                                                          view.addSubview(canvasView)
                                                         
                                                          if let sketch = sketch {
                                                              SketchService.downloadData(from: sketch.drawingUrl!) { (data) in
                                                                  self.canvasView.drawing = try! PKDrawing(data: data)
                                                                  canvasView.delegate = self
                                                                  self.saveButton.isEnabled = false
                                                              }
                                                          }
                                                         
                                                          canvasView.delegate = self
                                                          saveButton.isEnabled = false
                                                         
                                                          canvasView.translatesAutoresizingMaskIntoConstraints = false
                                                          view.addSubview(canvasView)
                                                         
                                                          canvasView.backgroundColor = .offWhite
                                                         
                                                          setupToolPicker()
                                                      }

                                                  and

                                                   

                                                      func setupToolPicker() {
                                                          if let window = self.parent?.view.window,
                                                              let toolPicker = PKToolPicker.shared(for: window) {
                                                              toolPicker.setVisible(true, forFirstResponder: canvasView)
                                                              toolPicker.addObserver(canvasView)
                                                              canvasView.becomeFirstResponder()
                                                              toolPicker.addObserver(self)
                                                              updateTools(toolPicker)
                                                          }
                                                      }

                                                   

                                                   

                                                  In my case:

                                                       override func viewDidAppear(_ animated: Bool) { 
                                                            
                                                            super.viewDidAppear(animated) // Manquait 
                                                            setTempData() 
                                                            if #available(iOS 13.0, *) { 
                                                                 let canvasView = PKCanvasView(frame: self.view.bounds) 
                                                                 guard 
                                                                 let window = view.window, 
                                                                 let toolPicker = PKToolPicker.shared(for: window) else { return } 
                                                                 
                                                                 toolPicker.setVisible(true, forFirstResponder: canvasView) 
                                                                 toolPicker.addObserver(canvasView) 
                                                                 canvasView.becomeFirstResponder() 
                                                                 view.addSubview(canvasView) 
                                                            } else { 
                                                                 // Fallback on earlier versions 
                                                            } 
                                                       }

                                                   

                                                   

                                                  So, your window is:

                                                  self.parent?.view.window

                                                  in my case:

                                                  view.window

                                                   

                                                  Why calling parent ?

                                                  Are you sure it is not nil ?