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

How to create vertical Scroll View in Xamarin iOS

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

下面是我的水平 ScrollView 代码

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


}
}

我想创建一个垂直滚动视图并添加一些可滚动的文本视图。关于如何做到这一点的任何想法?

如果您能详细说明我如何获取我的 Main.storyboard 文件中的元素,我将不胜感激,其中的结构方案如下滚动:

-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

还有更多...

您需要设置 ScrollView 内容大小高度,大于它的框架 height.So 它将滚动 up/down。滚动视图的宽度应与其内容宽度相同,因此它不会滚动 right/left。

我正在寻找这个。我使用了许多有代码和无代码的教程。进行滚动的最佳方法如下:

  • 添加scrollview(左、右、上、下约束)

  • 随意添加

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

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

    2) 垂直 space 到容器,其中容器是滚动视图(对最后一个元素执行此操作)

当您的屏幕变高时,垂直约束会增加您的滚动视图。无需代码,只需适当的约束。

在您的示例中,您必须:

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

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

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

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