代码之家  ›  专栏  ›  技术社区  ›  Kevin Amiranoff

PKCanvasView添加为子视图时不显示

  •  1
  • Kevin Amiranoff  · 技术社区  · 7 月前

    我正试图将PKCanvasView集成到React Native应用程序中,但当我尝试添加 canvasView 作为子视图,它不会显示。只有我直接退货,它才有效。

    这是我的密码。

    // BBNativePencilManager.swift
    // import PencilKit;
    
    @objc(BBNativePencilManager)
    class BBNativePencilManager: RCTViewManager {
      override func view() -> UIView {
    //    let canvasView = PKCanvasView();
    //    let drawing = PKDrawing()
    //    canvasView.drawing = drawing;
    //    if #available(iOS 14.0, *) {
    //      canvasView.drawingPolicy = .anyInput
    //    }
    //    if (theme == "dark") {
    //      canvasView.overrideUserInterfaceStyle = .dark;
    //    } else {
    //      canvasView.overrideUserInterfaceStyle = .light;
    //    }
    //    canvasView.isOpaque = false;
    //    canvasView.backgroundColor = .clear;
    //    canvasView.isMultipleTouchEnabled = true;
    //    canvasView.backgroundColor = .yellow
    //    canvasView.tool = PKInkingTool(.pen, color: UIColor.black, width: 5)
    //    canvasView.becomeFirstResponder()
    //    return canvasView;
    //    The above code works correctly
        return BBNativePencilView(); // <-- this does not work
      }
      
      override static func requiresMainQueueSetup() -> Bool {
        return true
      }
    }
    
    

    BBNativePencilView

    // BBNativePencilView.swift
    import UIKit
    import PencilKit
    
    
    class BBNativePencilView: UIView {
    
      override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(canvas)
      }
    
      required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
      }
      
      lazy var canvas: PKCanvasView = {
        let canvasView = PKCanvasView()
        let drawing = PKDrawing.init()
        canvasView.drawing = drawing;
        if #available(iOS 14.0, *) {
          canvasView.drawingPolicy = .anyInput
        }
        canvasView.isOpaque = false;
        canvasView.backgroundColor = .clear;
        canvasView.isMultipleTouchEnabled = true;
        canvasView.backgroundColor = .yellow
        canvasView.tool = PKInkingTool(.pen, color: UIColor.black, width: 5)
        canvasView.becomeFirstResponder()
        return canvasView;
      }()
    }
    
    

    有人能向我解释一下这个问题吗?非常感谢。

    1 回复  |  直到 7 月前
        1
  •  1
  •   Anita    7 月前

    这可能是由于将其添加为子视图时出现布局或框架问题。 确保BBNativePencilView框架和画布框架设置正确。如果画布的框架设置不正确或太小,则画布可能不可见。

    override init(frame: CGRect) {
        super.init(frame: frame)
        canvas.frame = self.bounds;
        canvas.autoresizingMask = [.flexibleWidth, .flexibleHeight];
        self.addSubview(canvas);
      }