在UIScrollView Xamarin.iOS

时间:2017-03-18 18:23:38

标签: uiscrollview xamarin.ios

我目前正在尝试将UIIView集中在UIScrollView中,并且在执行此操作时遇到了一些困难。

以下是我当前观点的图片:

UIViewController

以下是我正在使用的代码段:

public void AddView(UIViewController viewCont)
        {
            this.AddChildViewController(viewCont);
            this.mainScrollView.AddSubview(viewCont.View);
            viewCont.DidMoveToParentViewController(this);
        }

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

            // Perform any additional setup after loading the view, typically from a nib.
            var m = new Menu();
            //var c = new Camera();

            AddView(m);
            AddView(c);


            CGRect cFrame = c.View.Frame;
            cFrame.X = this.View.Frame.Width;
            c.View.Frame = cFrame;

            this.mainScrollView.ContentSize = new CGSize(this.View.Frame.Width * 2, 1.0);
        }

我想用Green填充整个视图但是你可以看到,View的底部四分之一并没有一直延伸到底部。

目前,我已经删除了所有约束,因为每次添加它们都会导致没有成功。我希望在这里得到一个具体的答案,我将如何在这个UIScrollView中集中这个视图。

由于

更新时间:3-21-2017

我的主要目标是在我的UIScrollView中并排放置2个ViewControllers,我可以使用滑动手势(如SnapChat)导航和导航。以下是@ Digitalsa1nt的建议,不幸的是我提出了同样的问题。

以下是一些图片:

第一个显示当我只添加1个视图时会发生什么:

UIViewControllerTwo

下一个显示当我尝试将两个视图添加到我的UIScrollView时会发生什么,只有相机显示:

CameraView

最后,这是我用来支持我的Camera视图的代码:

using Foundation;
using UIKit;
using AVFoundation;

namespace BRB.iOS
{
    public partial class Camera : UIViewController
    {
        AVCaptureSession captureSession;
        AVCaptureStillImageOutput stillImageOutput;
        AVCaptureVideoPreviewLayer previewLayer;

        public Camera() : base("Camera", null)
        {

        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use
        }

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            previewLayer.Frame = cameraView.Bounds;
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            captureSession = new AVCaptureSession();
            captureSession.SessionPreset = AVCaptureSession.Preset1920x1080;

            var backCamera = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);

            NSError error;
            var input = AVCaptureDeviceInput.FromDevice(backCamera, out error);

            if (error == null && captureSession.CanAddInput(input))
            {
                captureSession.AddInput(input);
                stillImageOutput = new AVCaptureStillImageOutput();
                stillImageOutput.OutputSettings = new NSDictionary(AVVideo.CodecKey, AVVideo.CodecJPEG);

                if (captureSession.CanAddOutput(stillImageOutput))
                {
                    captureSession.AddOutput(stillImageOutput);

                    previewLayer = new AVCaptureVideoPreviewLayer(captureSession);
                    previewLayer.VideoGravity = AVLayerVideoGravity.ResizeAspect;
                    previewLayer.Connection.VideoOrientation = AVCaptureVideoOrientation.Portrait;
                    cameraView.Layer.AddSublayer(previewLayer);
                    captureSession.StartRunning();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我通常会覆盖'didlayoutsubviews'来更改UIScrollViews中的视图。以下对我有用。

    public void AddView(UIViewController viewCont)
    {
        AddChildViewController(viewCont);
        mainScrollView.AddSubview(viewCont.View);
        viewCont.DidMoveToParentViewController(this);
    }

    public override void ViewDidLayoutSubviews()
    {
        base.ViewDidLayoutSubviews();
        // Ensure our contentinsets are 0 so we don't have any blank space
        mainScrollView.ContentInset = new UIEdgeInsets(0, 0, 0, 0);
        // set the contentsize to the bounds of the container view within.
        mainScrollView.ContentSize = View.Bounds.Size;
    }

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

        // Perform any additional setup after loading the view, typically from a nib.
        var m = new Menu();
        //var c = new Camera();

        AddView(m);
        AddView(c);
    }