Search code examples
cocoaswiftnsviewlayeravplayerlayer

Swift: How to add AVPlayerLayer to back layer of NSView


I have an NSView in interface builder like so:

Set up

I have buttons and a label inside the view (videoView) for playback control of a video. When I add an AVPlayerLayer (myPlayerLayer) to the view like so:

self.videoView.layer?.addSublayer(myPlayerLayer)

It adds the AVPlayerLayer as the top layer, thereby covering the buttons. How can I add the playerlayer to the back so it doesn't cover any of the other content?

Thanks a lot.


Solution

  • By doing:

    myPlayerLayer.zPosition = -1

    I forced the AVPlayerLayer to be behind any other existing layers already in the NSView. Note that setting zPosition to 0 does not work; I believe the buttons and labels were at 0 and so I needed to go further back.