C# 如何在Xamarin iOS中创建垂直滚动视图

C# 如何在Xamarin iOS中创建垂直滚动视图,c#,ios,xamarin,xamarin.ios,C#,Ios,Xamarin,Xamarin.ios,我是ios开发新手。我正在尝试在Xamarin iOS应用程序中创建垂直滚动视图 下面是我的水平滚动视图代码 using System; using UIKit; using Foundation; using CoreGraphics; using System.Collections.Generic; namespace TestApp { public partial class ViewController : UIViewController { publ

我是ios开发新手。我正在尝试在Xamarin iOS应用程序中创建垂直滚动视图

下面是我的水平滚动视图代码

using System;

using UIKit;
using Foundation;
using CoreGraphics;
using System.Collections.Generic;

namespace TestApp
{
    public partial class ViewController : UIViewController
    {

    public ViewController (IntPtr handle) : base (handle)
    {
        ScrollingButtonsController ();
    }

    UIScrollView scrollView;
    List<UIButton> buttons;

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.
        //MyScrollView.scrollEnabled=YES;
        //MyScrollView.contentSize= CGSizeMake(CGFloat.width, CGFloat.height);          

        //end
        nfloat h = 50.0f;
        nfloat w = 50.0f;
        nfloat padding = 10.0f;
        nint n = 100;

        scrollView = new UIScrollView {
            Frame = new CGRect (0, 100, View.Frame.Height, h + 2 * padding),
            ContentSize = new CGSize ((w + padding) * n, h),
            BackgroundColor = UIColor.Red,
            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 CGRect (padding * (i + 1) + (i * w), padding, w, h);
            scrollView.AddSubview (button);
            buttons.Add (button);
        }

        View.AddSubview (scrollView);

        //UIScrollView scrollView;
    //scrollView = new UIScrollView (
    //      new CGRect (0, 0, View.Frame.Width, View.Frame.Height));
    //  View.AddSubview (scrollView);



    }
    public void ScrollingButtonsController ()
    {
        buttons = new List<UIButton> ();

    }

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


}
}

还有更多…

您需要将ScrollView内容大小高度设置为大于其帧高度。因此它将向上/向下滚动。滚动视图的宽度应与其内容宽度相同,因此不会向右/向左滚动

我在找这个。我使用了很多有代码和没有代码的教程。执行滚动的最佳方法如下所示:

  • 添加滚动视图(左、右、上、下约束)

  • 添加您想要的任何内容

  • 棘手的部分是设置两个约束:

    1) 水平空间到容器,其中容器是滚动视图(使用更宽的元素执行此操作)

    2) 容器的垂直空间,其中容器是滚动视图(使用最后一个元素执行此操作)

当屏幕变得更高时,垂直约束将增加滚动视图。不需要代码,只需要适当的约束

在您的示例中,您必须:

  • 设置第一个标签的左右约束

  • 在所有标签之间添加垂直约束

  • 在最终文本视图和滚动视图之间添加最终垂直约束(文本视图可能需要高度约束)


如果您的元素在Xcode中不适合屏幕,请使用自由格式大小使控制器更大(这仅适用于Xcode,而不适用于实际程序)

请注意文本的格式和可读性。
-Scroll View
--label
---Image View
---Text View
--label
---Image View
---Text View
--label
---Image View
---Text View
--label
---Image View
---Text View
--label
---Image View
---Text View