c# - 如何在 Xamarin iOS 中创建垂直 ScrollView

标签 c# ios xamarin xamarin.ios

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

下面是我的水平 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.
    }


}
}

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

如果您能详细说明如何使用如下结构方案滚动我的 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 Content Size Height,大于它的框架高度。所以它会向上/向下滚动。 ScrollView 的宽度应与其内容宽度相同,因此它不会向右/向左滚动。

关于c# - 如何在 Xamarin iOS 中创建垂直 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34285130/

相关文章:

c# - MSTest - 从构建服务器隐藏一些单元测试

c# - HttpResponseMessage.Content.ReadAsStringAsync 不反序列化来自 CreateErrorResponse 的 JSON

c# - 在运行时对齐控件时对齐线

ios - 如果我淡出导航栏,然后实际上以编程方式隐藏它,我该如何做到这一点,如果我撤消它,就不会有时间问题?

Xamarin.Forms 删除页面

animation - Xamarin Forms - 如何在 StackLayout 中显示/隐藏项目的动画?

c# - Xamarin IOS : BLE

c# - Actor 计时器 - 注册计时器参数 - 为什么 Func<object, Task> 而不是 Func<Task>

ios - Swift:如何停止从循环开始的计时器?

ios - 来自数据库的 Realm 部分日期?