title: How Can I Make A CALayer And All Its Sublayers Zoom Proportionally?
body: <p>I am trying to make an NSScrollView which displays document pages. I have added a CALayer-backed NSView as the NSScrollView's documentView, and then I have added a CALayer sublayer to the documentView. When I zoom the NSScollview, the documentView correctly zooms in and out. However, the sublayers of the documentView do not scale proportionally with their containing documentView. If I do not set an autoresizingMask on the sublayers of the documentView layer, the sublayers simply fly off the screen when the documentView is zoomed. If I use the LayerWidthSizable/LayerHeightSizable options, the sublayers get way larger or smaller than they should be in reference to the documentView superlayer. Here is the code I have so far:</p>
  
  <p>Here is the NSScrollview:</p>
  
  <pre><code>class LayerScrollView: NSScrollView {
  
      var containerLayer: ContainerLayer!
  
      override func awakeFromNib() {
          documentView = ContainerLayer(frame: frame)
      }
  }
  </code></pre>
  
  <p>Here is the ContainerLayer (the documentView CALayer):</p>
  
  <pre><code>class ContainerLayer: NSView {
  
      let documentLayer: DocumentLayer = DocumentLayer()
  
      override init(frame frameRect: NSRect) {
          super.init(frame: frameRect)
          autoresizesSubviews = true
          wantsLayer = true
          layer = CATiledLayer()
          layer?.delegate = self
          layer?.backgroundColor = NSColor.blueColor().CGColor
          layer?.masksToBounds = true
          documentLayer.frame = CGRect(x: frame.width / 4.0, y: frame.height / 4.0, width: frame.width / 2.0, height: frame.height / 2.0)
          documentLayer.delegate = documentLayer
          layer?.addSublayer(documentLayer)
          documentLayer.autoresizingMask = CAAutoresizingMask.LayerWidthSizable | CAAutoresizingMask.LayerHeightSizable
          documentLayer.setNeedsDisplay()
      }
  
      required init(coder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
      override func drawLayer(layer: CALayer!, inContext ctx: CGContext!) {
          CGContextSetFillColorWithColor(ctx, NSColor.redColor().CGColor)
          CGContextFillRect(ctx, layer.bounds)
      }
  }
  </code></pre>
  
  <p>And finally, here is the DocumentLayer (the sublayers contained in the DocumentLayer):</p>
  
  <pre><code>class DocumentLayer: CALayer {
  
      override func drawLayer(layer: CALayer!, inContext ctx: CGContext!) {
          CGContextSetFillColorWithColor(ctx, NSColor.redColor().CGColor)
          CGContextFillRect(ctx, layer.bounds)
      }
  }
  </code></pre>
  
  <p>Here is a picture to illustrate the problem and my desired results:</p>
  
  <p><img src="https://i.imgur.com/W3eR1qR.png" alt=""></p>
  
  <p>The blue rectangle is the ContainerLayer, the red rectangle is the DocumentLayer.</p>
  
  <p>I have looked through numerous tutorials and the documents and come up empty. It seems like this should be super-easy to do. What am I missing here?</p>