Xamarin iOS StackView ScrollView

时间:2017-05-30 16:04:53

标签: ios xamarin scrollview stackview

我是Xamarin IOS的​​新手我尝试将stackView放入scrollView。然后我产生了这段代码。

public class ViewCategory : UIViewController, IIndicatorInfoProvider
{

    public override string Title { get; set; }
    public UIColor Color { get; set; }
    public string Text { get; set; }
    public string Image { get; set; }

    public UIScrollView _scrollView;
    public UIStackView _stackView;
    public ViewCategory(IntPtr handle) : base(handle) { }
    public ViewCategory(string title, UIColor color, string text, string image)
    {
        this.Title = title;
        this.Color = color;
        this.Text = text;
        this.Image = image;
    }

    public IndicatorInfo IndicatorInfoForPagerTabStrip(PagerTabStripViewController pagerTabStripController)
    {
        return new IndicatorInfo(Title);
    }

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

        _scrollView = new UIScrollView();

        _stackView = new UIStackView();


        _stackView.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, UIScreen.MainScreen.Bounds.Size.Height);
        _stackView.Axis = UILayoutConstraintAxis.Vertical;
        _stackView.Spacing = 10;





        // Image
        var image = new UIImageView(UIImage.FromBundle(this.Image));
        image.Frame = new Rectangle(0, 0, (int)View.Frame.Width, 150);
        image.ContentMode = UIViewContentMode.ScaleAspectFit;
        _stackView.AddArrangedSubview(image);

        //// define coordinates and size of the circular view
        //// define coordinates and size of the circular view

        float height = 80;
        float width = (float)UIScreen.MainScreen.Bounds.Size.Width * 2 + 40;
        float x = (width / 4 * -1) - 10;
        float y = 100 + ((100 - height) / 2);

        // corner radius needs to be one half of the size of the view
        RectangleF frame = new RectangleF(x, y, width, height);
        // initialize button
        UIButton circularView = new UIButton(frame);
        // set corner radius
        circularView.Layer.CornerRadius = width / 2;
        // set background color, border color and width to see the circular view
        circularView.BackgroundColor = UIColor.Blue;
        circularView.Layer.BorderColor = UIColor.White.CGColor;
        circularView.Layer.BorderWidth = 1;
        // add button to view controller
        _stackView.AddArrangedSubview(circularView);


        var pg_top_background = new UIImageView(UIImage.FromBundle("cat_gauge_top_background.png"));
        pg_top_background.Frame = new CGRect((UIScreen.MainScreen.Bounds.Size.Width - pg_top_background.Image.Size.Width) / 2, 150, pg_top_background.Image.Size.Width, pg_top_background.Image.Size.Height);
        _stackView.AddArrangedSubview(pg_top_background);

        var pg_top_foreground = new UIImageView(UIImage.FromBundle("cat_gauge_top_general.png"));
        pg_top_foreground.Frame = new CGRect((UIScreen.MainScreen.Bounds.Size.Width - pg_top_foreground.Image.Size.Width) / 2, 150, pg_top_foreground.Image.Size.Width, pg_top_foreground.Image.Size.Height);

        _stackView.AddArrangedSubview(pg_top_foreground);

        var pg_bottom_background = new UIImageView(UIImage.FromBundle("cat_gauge_bottom_background.png"));
        pg_bottom_background.Frame = new CGRect((UIScreen.MainScreen.Bounds.Size.Width - pg_bottom_background.Image.Size.Width) / 2, 150 + pg_top_foreground.Image.Size.Height + 5, pg_bottom_background.Image.Size.Width, pg_bottom_background.Image.Size.Height);
        _stackView.AddArrangedSubview(pg_bottom_background);

        var pg_bottom_foreground = new UIImageView(UIImage.FromBundle("cat_gauge_bottom_foreground.png"));
        pg_bottom_foreground.Frame = new CGRect((UIScreen.MainScreen.Bounds.Size.Width - pg_bottom_foreground.Image.Size.Width) / 2, 150 + pg_top_foreground.Image.Size.Height + 5, pg_bottom_foreground.Image.Size.Width, pg_bottom_foreground.Image.Size.Height);
        _stackView.AddArrangedSubview(pg_bottom_foreground);


        var label = new UILabel();
        label.Text = this.Text;
        label.Font = label.Font.WithSize(12);
        label.Frame = new CGRect(0, 180, 100, 20);

        //label.TextAlignment = UITextAlignment.Center;
        label.Center = new CGPoint(UIScreen.MainScreen.Bounds.Size.Width / 2, label.Frame.Y);
        _stackView.AddArrangedSubview(label);


        _scrollView.AddSubview(_stackView);
        _scrollView.Frame = new RectangleF(0, 0, (float)View.Frame.Width, (float)_stackView.Frame.Height);

        _scrollView.ContentSize = new SizeF((float)View.Frame.Width, (float)((float)_stackView.Frame.Height + 50));

        View.AddSubview(_scrollView);

    }
}

我不明白的一件事。为什么我必须将我的元素放在我的堆栈中,如果它感觉像是一个堆栈布局: 元素A. 元素B. 元素C

然后我绘制一幅图像,如果图像真的很大,其他元素就无法显示。

有人可以帮助我吗?

由于

0 个答案:

没有答案