I have a custom camera view in a UIViewController that does not take up the full screen. Beneath it is a collection view that fills up with little thumbnails of images taken with "X's" on then to allow the user to delete them before uploading the kept ones to firebase.
This all works lovely on IPhone 7 sized screens. However on the plus size phones, the preview layer of the custom camera view does not fill the entire UIView that the camera is placed in.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let deviceSession = AVCaptureDeviceDiscoverySession(deviceTypes:
[.builtInDuoCamera, .builtInTelephotoCamera, .builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .unspecified)
for device in (deviceSession?.devices)! {
if device.position == AVCaptureDevicePosition.back {
do {
let input = try AVCaptureDeviceInput(device: device)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
if captureSession.canAddOutput(sessionOutput) {
captureSession.addOutput(sessionOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = .portrait
cameraView.layer.addSublayer(previewLayer)
cameraView.addSubview(takePhotoButton)
previewLayer.position = CGPoint(x: self.view.frame.width/2, y: self.cameraView.frame.height/2)
previewLayer.bounds = cameraView.frame
captureSession.startRunning()
}
}
} catch let avError {
print(avError)
}
}
}
}
Here is where I add the preview layer as a sublayer of the CameraView which is just a UIView added in the interface builder with constraints to allow space in the view for the collection view beneath it.
previewLayer.position = CGPoint(x: self.cameraView.frame.width/2, y: self.cameraView.frame.height/2)
previewLayer.bounds = cameraView.frame
I don't understand why the preview layer added as a sublayer would behave any differently on an Iphone7 than an Iphone 7 Plus?? Especially because you can see on the Iphone 7 Plus, the grey around the preview layer which suggests that the CameraView is being constrained correctly, why would it's frame.width be different.
It seems that on the Plus size phone, the preview layer remains the same size as it would on a regular size phone, while the CameraView itself conforms to the larger phone screen correctly.
here is the View in question in the interface builder
And this is what seems to happen on a plus size phone


viewDidLayoutSubviews()method of the controller instead ofviewWillAppear()? This sounds like an Autolayout related issue. - 0xNSHumanpreviewLayer.positionandpreviewLayer.boundsinviewDidLayoutSubviews(). Your issue is most likely that at the timeviewWillAppear()executed, Autolayout didn't recalculatecameraView.framefor the actual device size, but uses the size you have set in your Storyboard.viewDidLayoutSubviews()method exists just for this purpose — use actual sizes recalculated for this particular device. - 0xNSHuman