在这个非常基本的UIScrollView

时间:2017-08-23 15:54:54

标签: ios xamarin uiscrollview

我试图让这个非常基本的UIScrollView水平滚动而不是垂直滚动。我在Xamarin开发者网站上关注这个例子:https://developer.xamarin.com/recipes/ios/content_controls/scroll_view/create_a_horizontal_scrolling_button_list/但是我不能让按钮占据整个滚动视图而只能水平滚动。我一直在按钮列表的顶部和底部得到奇怪的填充。这是我的全班:

public partial class StackViewController : UIViewController
{        
    public StackViewController()
    {            
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
    }

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

        this.View.BackgroundColor = UIColor.White;            

        nfloat h = 50.0f;
        nfloat w = 50.0f;
        nint n = 25;
        UIScrollView scrollView = new UIScrollView
        {
            Frame = new CGRect(0, 100, View.Frame.Width, 100),
            ContentSize = new CGSize((w) * n, h),
            BackgroundColor = UIColor.Green
        };
        for (int i = 0; i < n; i++)
        {                
            var button = new UIButton();
            button.SetTitle(i.ToString(), UIControlState.Normal);
            button.Frame = new CGRect((i * w), 0, w, h);
            button.BackgroundColor = UIColor.Blue;                
            scrollView.AddSubview(button);                
        }
        View.AddSubview(scrollView);
    }
}

This code produces this image

不知何故,我错过了一些我不了解的填充或布局属性。我是Xamarin ios开发的半新手,我知道我缺少一些简单的东西。

1 个答案:

答案 0 :(得分:1)

UIScrollviews通常可以添加指示符插入内容以及内容插入内容。我建议做的是覆盖'ViewDidLayoutSubviews'方法并输入以下代码:

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

     scrollView.ContentInset = UIEdgeInsets.Zero;
     scrollView.ScrollIndicatorInsets = UIEdgeInsets.Zero;
}