29

I've got a view controller in my storyboard with several UIButtons. One of them activates an AVFoundation camera preview layer shown in a sublayer:

captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.view.bounds;
[self.view.layer addSublayer:captureVideoPreviewLayer];

This works correctly except for the fact that the preview layer is rendered on top of my buttons so even though the buttons are still clickable they are not able to be seen by the user. Is there an easy way to place the sublayer below the buttons? Or an easy way to raise the buttons up in the layer? Thank you much!

golmschenk
  • 11,736
  • 20
  • 78
  • 137

2 Answers2

55

The button layers are all sublayers of the main view's layers. You'll need to put your camera preview layer below the button's layers. Try this:

// put it behind all other subviews
[self.view.layer insertSublayer:captureVideoPreviewLayer atIndex:0];

// or, put it underneath your buttons, as long as you know which one is the lowest subview
[self.view.layer insertSublayer:captureVideoPreviewLayer below:lowestButtonView.layer];
stevekohls
  • 2,214
  • 23
  • 29
  • The first of those worked. But just for future referece, how do I find out the name of the layers the buttons are on? – golmschenk Apr 16 '13 at 18:24
  • 1
    If you added your buttons in Interface Builder, you should connect them to an IBOutlet for the buttons in your view controller code. That way you have properties for them. Or, you could the the **tag** property in IB and then find them via the button view's tag. Did that answer your question? – stevekohls Apr 16 '13 at 18:27
  • I found ```[self.view.layer insertSublayer:captureVideoPreviewLayer below:lowestButtonView.layer];``` is working better for my case – air_bob Feb 12 '15 at 09:25
3

Just to add. You can raise any subclass of UIView up in the layer by calling UIView method bringSubviewToFront.

[self.view bringSubviewToFront:self.desiredButton];
Sergio
  • 1,702
  • 1
  • 20
  • 24