2
votes

Create the scroll view added to UIViewController View

UIScroll scroll_View;

scroll_View = new UIScrollView {
            BackgroundColor = UIColor.Black,
            Frame = View.Frame,
            ContentSize =  new SizeF(320,720),
        };
View.addSubView(scroll_View);// Added to Regisration ViewController

// Created text Fields

        firstName = new UITextField {
            Placeholder = "Enter firstName",
            BorderStyle = UITextBorderStyle.None,
            VerticalAlignment = UIControlContentVerticalAlignment.Center,
            AutocorrectionType = UITextAutocorrectionType.No,
            AutocapitalizationType = UITextAutocapitalizationType.None,
            ClearButtonMode = UITextFieldViewMode.WhileEditing,
            Background = TextFieldBackground,
            LeftView = new UIView (new RectangleF (0, 0,8, 8)),
            LeftViewMode = UITextFieldViewMode.Always,
            ReturnKeyType = UIReturnKeyType.Next,
            ShouldReturn = delegate {
                lastName.BecomeFirstResponder ();
                return true;
            }

        };

// Like this created 9 textfields and one submit button. added to ScrollView

Frame TextField.

firstName.Frame =   new RectangleF(80, 20, 200, 41);
lastName.Frame =    new  RectangleF(80, 70, 200, 41);
middle.Frame =  new  RectangleF(80, 120, 200, 41);
email.Frame =   new  RectangleF(80, 127, 200, 41);
password.Frame =    new  RectangleF(80, 220, 200, 41);
conformPassword.Frame = new  RectangleF(80, 270, 200, 41);
phoneNumber.Frame = new  RectangleF(80, 320, 200, 41);
description.Frame = new  RectangleF(80, 370, 200, 41);
other.Frame =   new  RectangleF(80, 420, 200, 41);
buttonSubmit.Frame =    new  RectangleF(80, 470, 420, 41);

Adding textfield to ScrollView

scroll_View.addSubView(firstName); 
scroll_View.addSubView(lastName); 
scroll_View.addSubView(middleName);
scroll_View.addSubView(email); 
scroll_View.addSubView(Password); 
scroll_View.addSubView(conformaPassword); 
scroll_View.addSubView(phoneNumber); 
scroll_View.addSubView(description); 
scroll_View.addSubView(other); 
scroll_View.addSubView(buttonSubmit); 

Added Scroll View UIViewController View.

    View.AddSubview (scroll_View);

When scrolling scrolling effect is not working. Xamarin IOS.

5
Try it in real device, I was have the same problem with UIDatePicker in simulator but in real device it scrolls. - Ravi_Parmar

5 Answers

3
votes
public override void ViewDidLoad ()
{

    base.ViewDidLoad ();

    float h = 50.0f;
    float w = 50.0f;
    float padding = 10.0f;
    int n = 25;

    _scrollView = new UIScrollView {
        Frame = new RectangleF (0, 0, View.Frame.Width, h + 2 * padding),
        ContentSize = new SizeF ((w + padding) * n, h),
        BackgroundColor = UIColor.DarkGray,
        AutoresizingMask = UIViewAutoresizing.FlexibleWidth
    };

    for (int i=0; i<n; i++) {
        var button = UIButton.FromType (UIButtonType.RoundedRect);
        button.SetTitle (i.ToString (), UIControlState.Normal);
        button.Frame = new RectangleF (padding * (i + 1) + (i * w), padding, w, h);
        _scrollView.AddSubview (button);
        _buttons.Add (button);
    }

    View.AddSubview (_scrollView);
}
2
votes

Try to use "ContentSize " property in UIScrollView.

  1. If you want to enable "Horizontal Scroll on UIScrollView" , need to increase the width of contentsize.

  2. If you want to enable "Vertical Scroll on UIScrollView" , need to increase the height of contentsize.

UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, 500, 300)]; scrollView.contentSize=CGSizeMake(1500, 200); //Horizontal scrolling

UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, 500, 300)]; scrollView.contentSize=CGSizeMake(200, 1500); //vertical scrolling

1
votes

Not a real answer yet it seems on this old question, but for future references: The problem in these cases is mostly that the ContentSize is equal to or smaller then the frame of the ScrollView. Therefore the ScrollView is unaware of the need to scroll.

In your case you should verify that the ContentSize of 320x720 is larger than the View.Frame you assign to the ScrollView Frame...

1
votes
public class FirstTab : UIViewController
{
    private  UIView content1;
    private  UIView content2;
    private  UIView content3;
    private  UIView containerView;
    private UIScrollView scrollView;
    CoreGraphics.CGSize contentViewSize;

    public FirstTab()
    {
        content1 = new UIView();
        content2 = new UIView();
        content3 = new UIView();
        containerView = new UIView();
        scrollView = new UIScrollView();
        contentViewSize = new CoreGraphics.CGSize(View.Frame.Width, View.Frame.Height + 800);

    }


    public override void ViewWillLayoutSubviews()
    {
        base.ViewWillLayoutSubviews();
        scrollView.ContentSize = contentViewSize;
    }

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

        content1.BackgroundColor = UIColor.Red;
        content2.BackgroundColor = UIColor.Black;
        content3.BackgroundColor = UIColor.Brown;







        Constraint();





    }

    private void Constraint()
    {

        containerView.AddSubviews(content1, content2, content3);
        containerView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
        containerView.AddConstraints(

            content1.Width().EqualTo(300),
            content1.Height().EqualTo(300),
            content1.AtTopOf(containerView).Plus(20),
            content1.WithSameCenterX(containerView),

            content2.Width().EqualTo(300),
            content2.Height().EqualTo(300),
            content2.Below(content1).Plus(20),
            content2.WithSameCenterX(containerView),

            content3.Width().EqualTo(300),
            content3.Height().EqualTo(300),
            content3.Below(content2).Plus(20),
            content3.WithSameCenterX(containerView)

            );

        scrollView.AddSubviews(containerView);
        scrollView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
        scrollView.AddConstraints(

            containerView.WithSameHeight(scrollView).Plus(300),
            containerView.WithSameWidth(scrollView)


            );

        View.AddSubviews(scrollView);
        View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
        View.AddConstraints(

            scrollView.WithSameWidth(View),
            scrollView.WithSameHeight(View).Plus(400)

            );

    }
}

UIScrollView will work when You put the ScrollView.ContentSize into ViewWillLayoutSubview() method.

It took me very long time to find this simple trick for a working UIScrollView. I have used Cirrious.FluentLayout for autolayout.

-2
votes

In this case u need better work with UITableView not UIScrollView.