AVCaptureVideoPreviewLayer displays correctly except on iPhone6

I'm working on implementing a view that consists of an AVCaptureVideoPreviewLayer. The view is supposed to take up the entirety of the screen, except for the navigation bar at the top (the view is embedded in a navigation controller).

My code below shows how I've set up a capture session and added the front camera as input if it's available.

//Set up capture Session session = [[AVCaptureSession alloc]init]; session.sessionPreset = AVCaptureSessionPresetPhoto; //Add a camera as input if we have a front-facing camera available NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; NSError *error; for (AVCaptureDevice *device in devices) { if ([device position] == AVCaptureDevicePositionBack) { //Set our input AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; [session addInput:input]; if (!input) { NSLog(@"No Input"); } } }

Below is where the output is configured, and the camera output view is added as a subview, with the frame determined by the view's bounds.

//Output AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; [session addOutput:output]; output.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) }; //Preview Layer AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; previewLayer.frame = self.view.bounds; self.view.translatesAutoresizingMaskIntoConstraints = NO; previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; [self.view.layer addSublayer:previewLayer];

As you can see in the screenshot below when running on an iPhone 6, the preview layer does not take up the entirety of the screen (the white space to the right of the camera view and under the navigation bar should be filled up by the preview layer):

AVCaptureVideoPreviewLayer displays correctly except on iPhone6

However, it does fill up the screen appropriately on the iPhone 5 screenshot below (as well as the iPhone 4, 4S, and 5S). Does anyone have any insight into why?

AVCaptureVideoPreviewLayer displays correctly except on iPhone6


Assuming you're creating this video preview layer in viewDidLoad, the view controller's view has not been laid out yet, and as such its frame and bounds are not necessarily what they will be once the view is shown on screen. You'll have to wait until the view is laid out to set the correct frame of your preview layer, which you can do by implementing the viewDidLayoutSubviews method of your view controller, like so:

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.previewLayer.frame = self.view.bounds;

This ensures that any time your view controller's view changes in size, your preview layer also changes in size.

You can use an AVCaptureVideoPreviewLayer as the backing layer of a UIView subclass, and then add an instance of this view as a subview to your view controller's view. This way you can use constraints to position your video capture preview however you want, as you would with any other view.

Here's an example of how you could do that:

// MyCaptureVideoPreviewView.h

#import <AVFoundation/AVFoundation.h>

@interface MyCaptureVideoPreviewView : UIView

@property (nonatomic, readonly) AVCaptureVideoPreviewLayer *layer;
@property (nonatomic) AVCaptureSession *session;

- (instancetype)initWithFrame:(CGRect)frame session:(AVCaptureSession *)session;


// MyCaptureVideoPreviewView.m

@implementation MyCaptureVideoPreviewView

@dynamic layer;

+ (Class)layerClass {
return [AVCaptureVideoPreviewLayer class];

- (AVCaptureSession *)session {
return self.layer.session;

- (void)setSession:(AVCaptureSession *)session {
self.layer.session = session;

- (instancetype)initWithFrame:(CGRect)frame session:(AVCaptureSession *)session {
self = [super initWithFrame:frame];
if (self) {
self.session = session;
return self;


And then somewhere in your view controller's implementation:

MyCaptureVideoPreviewView *previewView = [[MyCaptureVideoPreviewView alloc] initWithFrame:self.view.bounds
previewView.layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:previewView];

// Add whatever constraints you want. Here we just match the superview's bounds:
NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[previewView]|" options:kNilOptions
metrics:nil views:NSDictionaryOfVariableBindings(previewView)];
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[previewView]|" options:kNilOptions
metrics:nil views:NSDictionaryOfVariableBindings(previewView)];

[self.view addConstraints:vConstraints];
[self.view addConstraints:hConstraints];

Category:ios Time:2018-12-09 Views:1

Related post

  • desktop icons are not displaying correctly 2014-03-30

    Recently, I installed and uninstalled some softwares ( i think custopack and window 8 transformation pack) Now my desktop icons are not displaying correctly except Computer, Recycle bin and control panel as follow https://skydrive.live.com/redir.aspx

  • Exception message not being displayed correctly on client computers 2011-09-08

    The project I'm currently working on is a form designer (Silverlight Application) wherein the user can drag a control from the toolbox to the work canvas and then supply its properties in the property pane (like Visual Studio and Expression Blend) We

  • Customized NavigationBar not displaying correctly after return to Tab 2010-02-27

    I've added a few components to the NavigationBar in an app I'm creating. The height of the bar changes dynamically, like in Safari for the iPhone, when you click on the URL field. I have all of the interactions and resizings working perfectly, except

  • Why does my page not display correctly in Internet Explorer 6? 2011-04-02

    I created a simple page with a series of divs. In all of the modern browsers, it appears fine, but in Internet Explorer 6, it falls apart. I have no idea what is causing it to happen. You can view the page here: IE 6 Test Page If you have IE6 install

  • IE Font styles only display correctly until page finishes loading 2011-08-09

    Very strange problem on a site I'm working on: http://storyhaus.com/ The fonts I have used on the site (Georgia and Bebas Neue) display correctly while the page is loading, however once the page is fully loaded they fall back to standard sans fonts.

  • Recaptcha doesn't display correctly in Chrome 2012-03-04

    I have included Recaptcha in a form on my site. It works correctly and displays correctly in all browsers except Google Chrome where the theme is broken some how. I suspected it was an issue with the iframe and tried iframe{display:none;} but this di

  • Windows Explorer Address Bar Refresh Icon Not Displayed Correctly 2012-07-24

    What's up with the Address bar Refresh icon? It's skewed and not displayed correctly until I hit it to refresh afterwhich it then aligns correctly. It's actually the entire address bar that is shifted down, but mainly noticeable on the Refresh button

  • Webpage won't display correctly on New Laptop 2012-12-17

    Hi Everyone, I would greatly appreciate if someone can help me resolve this problem. I've been trying to figure it out since last week. I purchased a new Lenovo Yoga 2 11 Laptop recently and it cannot display a calorie tracking website that I use fre

  • bing maps doesn't display correctly 2012-04-23

    Sometimes map will display for a second when zooming but then disappears. Sometimes parts of map will display at certain zoom level then disappear when I drag to move. Happens with either road map or aerial view. Have uninstalled and reinstalled app

  • Contacts not displaying correctly 2013-04-13

    Hi everyone. Happy Memorial Day. I'm hoping someone can help me, I can't find a solution. I just set up Outlook 2010 (desktop) with my Hotmail account and the contacts are not displaying correctly. I have each individual contact to display in order b

  • Xbox live avatar not displaying correctly - shows up as a black box 2013-08-14

    I have an HTC surround and everything works great, except that my xbox live avatar no longer displays correctly. It used to show up just fine, but now, in place of my avatar, i just see a big black box. I'm able to connect with xbox live just fine, a

  • Manage add-ons UI window does not display correctly 2013-10-05

    Manage add-ons UI window does not display correctly. The area where the add-on information is supposed to be displayed in the Manage Add-on window is compressed down to 1/2 inch. Running Windows 7 Home Premium with IE10 and all updates are current. R

  • Msnbc.msn.com not displaying correctly with IE9 2014-03-22

    I asked this before, but the problem seems not solved yet. Again, the top bar on Msnbc.msn.com that shrinks or opens when scrolled up or down doesn't display correctly and the website itself is very slow. Meanwhile, Safari and Chrome are just fine in

  • Windows 7 gadgets may stop working or it is not displayed correctly 2014-10-22

    I did system restore few days ago and now my gadgets are not working properly. I tried using most of the advice in the forums, except the one is regedit because i am not comfortable with it. I also have tried microsoft support (fix it) but still it i

  • Image problems on IE with Vista. Most images are not displaying correctly showing a red x. 2014-11-21

    Hi there, i'm having a problem with IE on Vista. Most images are not displaying correctly showing a red x. It seems to be png images causing most of the problem but even the main google image on there homepage won't display. If you look at the igoogl

  • Web pages not displaying correctly after Windows RT upgrade to 8.1 and IE 11 2014-11-24

    After upgrading to Windows 8.1 on the Surface RT some web pages that worked before the upgrade do not display correctly. For example, on pages that uses a scroll box, the contents of the box extends out of the box or the contents of the box are trunc

  • ATL ActiveX control not displaying correctly in IE9 beta 2014-12-18

    We have an ActiveX control, developed with ATL 9.0 / vs2008, does not display correctly: disappears when clicked on, reappears when IE window is resized. Switching to compatibility mode seems to overcome the problem. Simple ATL control created using

  • Why are my Chinese characters not displayed correctly in c# string 2009-05-29

    I am storing Chinese and English text in an SQL Server 2005 database and displaying it on a webpage, but the Chinese is not being displayed correctly. I have been reading about the subject and have done the following: used N before the text in my INS

  • Make python __doc__ property display correctly in a Windows CMD window? 2009-08-01

    In Windows, if I open a command prompt, start python, and inspect something using its __doc__ property, it doesn't display correctly. Instead of the lines being separated, I see one continuous string with the newline character every once and a while.

  • Greek text not displayed correctly 2009-08-04

    We have an application that used the C++ zApp framework for UI (forms, fonts, everything). We have slowly converted it to use the .net framework and recently found that Greek characters are no longer displaying correctly. In one version of the applic

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 1.780 (s). 13 q(s)