Scrollview Fluent 布局中的子视图

Subview in Scrollview Fluent layout

我刚开始在 Xamarin.iOS 项目中使用 Fluent 布局,现在我偶然发现了一个我不知道如何解决的问题。

情况是这样的:

我有一个滚动视图作为主视图,里面有一些文本,现在我想添加另一个包含其他文本的视图(我们称之为 "subView")。 为什么我想要 "subView" 是因为我有一个 "Hide/Show" 按钮可以隐藏或显示 "subView".

我应该在哪里添加用于在 "subView" 中定位内容的约束?在 subView.AddConstraints() 内,或在 mainView.AddConstraints?

我不知道该怎么做,有人可以帮我吗?

Example of what im doing

Where'm I suppose to add the constraints for positioning the stuff inside "subView"? Inside subView.AddConstraints(), or in mainView.AddConstraints?

视图被分配了布局其直接子视图的约束,因此 subView 内的控件应使用 subView.AddConstraints() 布局。如果subViewmainView的子视图,那么应该用mainView.AddConstraints().

布局

编辑:一个例子:

mainView.Add(subView);
subView.Add(someOtherView);

var myPadding = 12f;

mainView.AddConstraints(new FluentLayout[]
{
    subView.AtTopOf(mainView, myPadding),
    subView.AtLeftOf(mainView, myPadding),
    subView.AtRightOf(mainView, myPadding),
    subView.AtBottomOf(mainView, myPadding)
});

subView.AddConstraints(new FluentLayout[]
{
    someOtherView.AtTopOf(subView, myPadding),
    someOtherView.AtLeftOf(subView, myPadding),
    someOtherView.AtRightOf(subView, myPadding),
    someOtherView.AtBottomOf(subView, myPadding)
});

隐藏和显示视图是一个单独的问题 - 当执行显示或隐藏的操作时,您需要销毁并重新创建单独的约束 subView,或者使用 MvvmCross 将某些约束绑定到 ViewModel 属性,这您可以在 .

上找到说明

尝试使用此代码:

CGRect rect = new CGRect(0, UIApplication.SharedApplication.StatusBarFrame.Height, UIScreen.MainScreen.ApplicationFrame.Width , UIScreen.MainScreen.ApplicationFrame.Height);
UIScrollView Mainscrollview = new UIScrollView(rect) { BackgroundColor = UIColor.Gray };
View.AddSubview(Mainscrollview);
this.View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();

var someLablel = new UILabel();
var button = new UIButton { BackgroundColor = UIColor.Gray };
var blueView = new UIView { BackgroundColor = UIColor.Blue };
Mainscrollview.AddSubviews( someLablel,button, blueView);

Mainscrollview.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();


Mainscrollview.AddConstraints(
     someLablel.AtTopOf(Mainscrollview, 50),
     someLablel.AtLeftOf(Mainscrollview, 10),
     someLablel.AtRightOf(Mainscrollview, 10),
     someLablel.Height().EqualTo(50),

     button.AtBottomOf(someLablel, 10),
     button.WithSameLeft(someLablel),
     button.WithSameRight(someLablel),
     button.WithSameHeight(button),

     blueView.AtBottomOf(button, 10),
     blueView.WithSameLeft(someLablel),
     blueView.WithSameRight(someLablel),
     blueView.AtBottomOf(Mainscrollview, 10)
);

//*do as above*
//blueView.AddSubviews(SubViewLabel1, Another , more);
//blueView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
//blueView.AddConstraints( );

PS:

如上所说,FluentLayout是用来描述parenet view和subview或者subview和subview(同一个parent view)的关系。我们无法将子视图与它的超级超级视图相关联(例如,图片中的 subviewLabel 和 Mainscrollvew)。而且最重要的是我们添加的Constraints必须足够,也就是说view必须通过这些Constraints得到它的位置和大小。