UIScrollView滚动Xamarin IOS不工作

本文关键字:工作 IOS Xamarin 滚动 UIScrollView | 更新日期: 2023-09-27 18:13:23

创建添加到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

//创建的文本字段

        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;
            }
        };

//像这样创建了9个文本字段和一个提交按钮。已添加到ScrollView

框架文本字段。

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);

将文本字段添加到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); 

添加了滚动视图UIViewController视图。

    View.AddSubview (scroll_View);

当滚动效果不起作用时。Xamarin IOS。

UIScrollView滚动Xamarin IOS不工作

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);
}

尝试在UIScrollView中使用"ContentSize"属性。

  1. 如果你想启用"UIScrollView上的水平滚动",需要增加内容大小的宽度。

  2. 如果你想启用"UIScrollView上的垂直滚动",需要增加内容大小的高度。

UIScrollView*scrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,40500300(];scrollView.contentSize=CGSizeMake(1500200(//水平滚动

UIScrollView*scrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,40500300(];scrollView.contentSize=CGSizeMake(2001500(//垂直滚动

这似乎不是一个真正的答案,但对于未来的参考:这些情况下的问题主要是ContentSize等于或小于ScrollView的框架。因此,ScrollView不知道需要滚动。

在您的情况下,您应该验证ContentSize 320x720是否大于您分配给ScrollView Frame的View.Frame。。。

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)
            );
    }
}

当您将ScrollView.ContentSize放入ViewWillLayoutSubview((方法时,UIScrollView将起作用。

我花了很长时间才为一个正在工作的UIScrollView找到这个简单的技巧。我使用了Circly.FluentLayout进行自动布局。

在这种情况下,您需要更好地使用UITableView而不是UIScrollView