updateLayer not called on layer backed NSView

I have the following `NSView` subclass, which is layer backed, and draws a `CAGradientLayer`. When I call `needsDisplay = true`, I'd expect `updateLayer` to be called, but it isn't. Is my understanding of what should happen incorrect, or have I done something wrong?


Thanks,


Luke


class ViewSubclass: NSView
{
     override var wantsUpdateLayer: Bool
     {
          return true
     }

     init()
     {
          super.init(frame: CGRectZero)

          self.wantsLayer = true
          self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay
     }

     required init?(coder: NSCoder)
     {
          fatalError("init(coder:) has not been implemented")
     }

     override func makeBackingLayer() -> CALayer
     {
          let saturationGradientLayer = CAGradientLayer()
          saturationGradientLayer.colors = [NSColor.whiteColor().CGColor,
               NSColor.redColor().CGColor]
          saturationGradientLayer.locations = [0.0, 1.0]
          saturationGradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
          saturationGradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)

          return saturationGradientLayer
     }

     override func updateLayer()
     {
          NSLog("Update layer called")
     }

How are you instantiating the view? In code or a xib file?

Update layer will only be called if you use ther apple internal backing layer and not overriding makeBackingLayer(), else NSView behaves layer hosted not backed.

Did you:


1) Override wantsUpdateLayer to return true?


    override var wantsUpdateLayer: Bool {
        get{
                 return true
            }
    }


2) Set the layerContentsRedrawPolicy?


self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay;



No 2 above isn't necessary, but I use it as a force of habit. Either way, No 1 above will call updateLayer when "needsDisplay" is set to true

I wanted to update it (after 5 years) that NSView.updateLayer() is still not called despite all the magic:

Code Block swift
override var wantsUpdateLayer: Bool { true }
wantsLayer = true
layerContentsRedrawPolicy = .onSetNeedsDisplay


Code Block
override func makeBackingLayer() -> CALayer {
CATextLayer()
}


without custom makeBackingLayer, updateLayer() is called.

updateLayer not called on layer backed NSView
 
 
Q