title: SpriteKit SKScene add AVCaptureVideoPreviewLayer CALayer as background not working
body: <p>I'm attempting to add the AVCaptureVideoPreviewLayer CALayer as the background to my SKScene. I can add the CALayer to the scene, but no matter what attempts at ordering the CALayer is always the top most object. </p>
  
  <p>In the didMoveToView I have the function:</p>
  
  <pre><code>- (void) didMoveToView:(SKView *)view
  {
      [self addVideo];
  
  } 
  </code></pre>
  
  <p>Which calls the addVideo function:</p>
  
  <pre><code>-(void) addVideo {
  
  AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
  captureSession.sessionPreset = AVCaptureSessionPresetHigh;
  
  AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
  
  [captureSession addInput:videoIn];
  
  AVCaptureVideoPreviewLayer *avLayer =
  [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
  avLayer.frame = self.view.bounds;
  
  CGSize landscapeSize;
  landscapeSize.width = self.view.bounds.size.height;
  landscapeSize.height = self.view.bounds.size.width;
  
  
  CGRect landscapeRect;
  landscapeRect.size = landscapeSize;
  landscapeRect.origin = self.view.bounds.origin;
  
  avLayer.frame = landscapeRect;
  
  if(avLayer.connection.supportsVideoOrientation)
  {
      avLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
  }
  
  [self.scene.view.layer insertSublayer:avLayer atIndex:0];
  [self.scene.view.layer setNeedsLayout];
  
  
  [captureSession startRunning];
  </code></pre>
  
  <p>}</p>
  
  <p>I've attempted multiple ways to get that AVCaptureVideoPreviewLayer CALayer as the background. Everytime a node is added to the scene it is above this CALayer, any advice?</p>
  
  <p>Attempted the following function:</p>
  
  <pre><code>- (void) sendSublayerToBack:(CALayer *)layer
  {
     [layer removeFromSuperlayer];
      [self.view.layer insertSublayer:layer atIndex:0];
  }
  </code></pre>
  
  <p>But still not working....</p>