如何正确地从代码创建UIStackView

时间:2016-09-30 15:37:59

标签: ios xamarin.ios

我无法理解为什么这种布局不会呈现我需要它的方式。用linearlayout完成对android的要求应该等同于这个吗?

我得到了什么:

What i get

我需要什么:

What i need

我的代码:

        // my stackview only renders at all if i pass any height > 0 && width > 0, 
        // but i would love this to be as small as possible, given that all children 
        // are rendered.
        var stackView = new UIStackView(View.Frame);
        stackView.Axis = UILayoutConstraintAxis.Vertical;
        stackView.Distribution = UIStackViewDistribution.Fill;

        foreach (var deviceVm in devices)
        {
            // i would prefer this not to have a rect parameter and scale as needed.
            var descriptionLabel = new UILabel(new CGRect(0, 0, View.Bounds.Width, 20));
            descriptionLabel.Text = deviceVm.Description;
            descriptionLabel.TextColor = UIColor.Blue;
            descriptionLabel.ContentMode = UIViewContentMode.ScaleToFill;
            stackView.AddArrangedSubview(descriptionLabel);
        }

        // i assume this should make it as small as possible, as big as necessary?
        stackView.SizeToFit();

        var scrollView = new UIScrollView(View.Frame);
        scrollView.BackgroundColor = UIColor.Yellow;
        scrollView.AddSubview(stackView);
        View.AddSubview(scrollView);

有人发现我在这里犯的错误吗?我是不正确配置StackView还是这个控件不等同于LinearLayout(android)/ StackPanel(wpf)?

PS:对于眼部癌症涂料图片感到抱歉。

0 个答案:

没有答案