0
votes

I created a form in Xamarin.Forms and then used a PageRenderer in Xamarin.iOS to display live camera feed using AVCaptureSession. I set a UIView Frame equal to the View.Frame and then added a sublayer AVCaptureSessionPreviewLayer to that UIView. Testing on an iPhone 7 the Camera feed takes up the entire screen but testing on an IPad the camera feed is only in the middle leaving a big margin on the left and right, but not on the top or bottom. When debugging the Bounds that are set they equal 0, 0, 768, 1024. Even though the x and y origin are both 0 it does not start in the top left corner.

How can I fix this issue? Is there a way instead of creating a completely new UIView in the code behind I can just set the AVCaptureSessionPreviewLayer to show inside of a Xamarin.Forms control?

UPDATE: I added another control and set the origin to 0,0 and it's in the top left corner. So I would assume that me adding a sublayer is the cause of the improper origin and width.

1
When were you performing the drawing? You will likely have different results doing drawing if you execute the same code in ViewDidLoad, ViewWillAppear, ViewDidLayoutSubviews since the view hasn't figured out it's correct dimensions until ViewWillAppear. Something to be mindful of! - Axemasta
I tried moving my code that initializes the UI to ViewWillAppear from ViewDidLoad and it did not change anything unfortunately. Thanks for the suggestion though! - Shawnzey

1 Answers

0
votes

You need to set the layer's frame equal to its super layer's bounds, just as you do with the view. You should do this in the layoutSubviews method of the parent view, so that the layer responds to any auto layout size changes in the parent view.