I have two view controllers. One is the root VC and contains the UI interface such as the record button. On this view controller, I also display the view of another VC at index 0. This view contains a AVCaptureVideoPreviewLayer.
I would like my video camera to mimic the Apple video camera app, where the interface layout adjusts with the rotation, but the video preview layer does not. You can see how the recording timer (UILabel) in the stock video app disappears and reappears at the top depending on the orientation.
Any idea how to do this? I found one suggestion that recommendeds adding the preview to the app delegate's window, since it won't conform to the rotation of the nav controller, but it didn't work for me.
Thanks!
On the view that shows the camera output add:
AVCaptureVideoOrientationPortrait is just one option. You can choose from the following:
This must be done after you setup the session.
Make sure to set shouldAutorotate to return false:
register for Notifications that orientation changed:
implement the notification change
and rotate the UI
This is how I implement the screen being locked but rotating the UI, if this works link the stacks post and I can copy it over there and you can tick it :P
I have a very similar situation. I just have one view controller and I want to have a
AVCaptureVideoPreviewLayer
that doesn't rotate in it. I found the accepted solution by @SeanLintern88 did not work for me; the status bar never moved and the WKWebView I had on the screen was not getting resizes properly.One of the bigger issues I ran into was that I was putting my
AVCaptureVideoPreviewLayer
in the view controller's view. It is much better to create a newUIView
just to hold the layer.After that I found a technical note from Apple QA1890: Preventing a View From Rotating. This allowed me to produce the following swift code:
The original tech note did not have the line
self.previewView!.layer.frame = self.view.bounds
but I found that very necessary because although the anchor point doesn't move, the frame has. Without that line, the preview will be offset.Also, since I am doing all of the work keeping the view in the correct position, I had to remove all the positioning constraints on it. When I had them in, they would cause the preview to instead be offset in the opposite direction.