DerStarkeBaer
DerStarkeBaer

Reputation: 669

UIButton are not visible over camera stream (Xamarin.iOS)

I made an Xamarin.iOS app where I have a camera stream. In my storyboard I have a UIButton to take a picture and one to switch the cameras.

But when I start my app the buttons are under the camera stream.

Does someone know how to place the buttons over the camera stream?

Here is my code

public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        liveCameraStream = PhotoView;

        SetupLiveCameraStream();
    }

    public void SetupLiveCameraStream()
    {
        captureSession = new AVCaptureSession();

        var viewLayer = liveCameraStream.Layer;
        var videoPreviewLayer = new AVCaptureVideoPreviewLayer(captureSession)
        {
            Frame = liveCameraStream.Bounds
        };
        liveCameraStream.Layer.AddSublayer(videoPreviewLayer);

        var captureDevice = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Video);
        ConfigureCameraForDevice(captureDevice);
        captureDeviceInput = AVCaptureDeviceInput.FromDevice(captureDevice);

        var dictionary = new NSMutableDictionary();
        dictionary[AVVideo.CodecKey] = new NSNumber((int)AVVideoCodec.JPEG);
        stillImageOutput = new AVCaptureStillImageOutput()
        {
            OutputSettings = new NSDictionary()
        };

        captureSession.AddOutput(stillImageOutput);
        captureSession.AddInput(captureDeviceInput);
        captureSession.StartRunning();
    }

Here you can see the storyboard with the buttons storyboard of my app

Upvotes: 0

Views: 164

Answers (1)

MilanG
MilanG

Reputation: 2604

Here liveCameraStream View hovers above the Buttons so we have to send it back in the View Hierarchy so that Buttons can be viewed.

Any one of the below should work for you:

// 1
UIView cameraView = new UIView(videoPreviewLayer.Frame);
cameraView.Layer.AddSublayer(videoPreviewLayer);
View.AddSubview(cameraView);
View.SendSubviewToBack(cameraView);

-------OR-------

// 2
liveCameraStream.Layer.AddSublayer(videoPreviewLayer);
View.SendSubviewToBack(liveCameraStream);

Replace the following line with above lines of code:

liveCameraStream.Layer.AddSublayer(videoPreviewLayer);

EDIT:

2nd option worked for this post.

Upvotes: 1

Related Questions