Scrollview Fluent布局中的子视图

时间:2017-08-16 06:44:53

标签: c# xamarin xamarin.ios mvvmcross cirrious.fluentlayout

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

情况是这样的:

我有一个scrollview作为mainView和其中的一些文本,我现在想要添加包含其他文本的另一个视图(让我们称之为“subView”)。 为什么我想要“subView”是因为我有一个“隐藏/显示”按钮,假设隐藏或显示“子视图”。

我想在“subView”中添加用于定位内容的约束?在subView.AddConstraints()或mainView.AddConstraints中?

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

Example of what im doing

2 个答案:

答案 0 :(得分:0)

  

我想在哪里添加用于定位内容的约束" subView"?在subView.AddConstraints()或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属性,您可以查找有关here的说明。

答案 1 :(得分:0)

enter image description here

尝试使用此代码:

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视图与子视图或子视图和子视图(具有相同的父视图)之间的关系。我们无法将子视图与它的超级超视图相关联(例如,pic中的subviewLabel和Mainscrollvew)。而最重要的是什么是我们添加的约束必须足够,这意味着视图必须使用这些约束来获取其位置和大小。