UIScrollView 将元素从它们的设置位置移动

UIScrollView moves elements from their set positions

我正在创建 IOS 应用程序供我组织中的所有工程师查看和完成他们的工作。但是,当使用 UIScrollView 时,它会将内容移离我在故事板上放置它的位置。

使用普通的 UIView(不是滚动),元素被放置在我放置它们的位置,但是如果页面上有大量开关(动态地),它可能会溢出,所以我需要能够滚动项目.

我正在使用storyboard here。创建我的主要布局。 但是,在我的滚动布局中,我使用此代码添加按钮。

y = StartY
x = StartX

foreach (var component in DetailItem.Components)
{
    y += yPls;
    UISwitch sw = new UISwitch(new CoreGraphics.CGRect(x, y, 51, 31));
    sw.OnTintColor = UIColor.Yellow;
    sw.ThumbTintColor = UIColor.Blue;

    sw.ValueChanged += (sender, e) => {
        var comp = sender as UISwitch;
        DetailItem.Components[component.Key] = comp.On;
    };

    UILabel lbl = new UILabel(new CoreGraphics.CGRect(x + 55, y, 200, 31));
    lbl.Text = component.Key;
    this.Add(sw);
    this.Add(lbl);
}

问题是元素正在从我放置它们的位置移动,如下所示:

我目前选择了 "Job Name:" 标签,但如您所见,它正在移动。

任何对此的帮助或关于为什么会发生的想法都很棒!

Select Storyboard 中的 ViewController 然后在 Properties -> Widget -> Extend Edges 中,有一个名为 [=30 的选项=]在 Top Bars 下,取消选中它,scrollView 中的元素将保持在正确的位置。

这是截图:

后面的代码应该是:

this.EdgesForExtendedLayout = UIRectEdge.None or UIRectEdge.None.all or UIRectEdge.Bottom;

edgesForExtendedLayout 告诉应该扩展哪些边缘(左、右、上、下、全部、none 或它们的任意组合)。延伸底部边缘等于 "Under Bottom Bars" 个刻度,延伸顶部边缘等于 "Under Top Bars" 个刻度。

查看 document and this thread 了解更多信息。