简体   繁体   English

将视图控制器添加到具有约束的uiview中

[英]Adding view controller to uiview with constraints

In a storyboard, I have a 3 screens (controller view): 1) Controller "main" containing a uiview named cntdetail and a button named viewswitch 2) Controller "A" with several elements (label, textfields...) 3) Controller "B" with also several elements 在情节提要中,我有3个屏幕(控制器视图):1)控制器“ main”包含名为cntdetail的uiview和名为viewswitch的按钮2)控制器“ A”具有多个元素(标签,文本字段...)3)控制器“ B”也有几个元素

What I need it to load the view A in cntdetail when the view appears and switch from A to B when tapping the button and vice versa. 我需要什么来在视图出现时以cntdetail加载视图A并在点击按钮时从A切换到B,反之亦然。

What I'm doing (it is in C# but nearly like swift): 我在做什么(在C#中,但几乎像swift一样):

In the Main ViewDidLoad: 在主ViewDidLoad中:

cviewA = Storyboard.InstantiateViewController("SaisieVocale");
cviewB = Storyboard.InstantiateViewController("SaisieDetaillee");
AddChildViewController(cviewA);
AddChildViewController(cviewB);
viewAcontenth = GetContentHeight(cviewA.View); //a personal function that calculate the height of the content, it works
viewBcontenth = GetContentHeight(cviewB.View);

when clicking the button (and nearly the same in viewdidappear) (I use A to simplify but it is once A once B): 单击按钮时(与viewdidappear中的按钮几乎相同)(我使用A来简化,但一次是A一次是B):

cviewB.View.RemoveFromSuperview();

//change the height constraint of the uiview to fit the height of my views 
        foreach (var cst in cntdetails.Constraints)
        {
            if ((UIView)cst.FirstItem == cntdetails || (UIView)cst.SecondItem == cntdetails)
            {
                if (cst.FirstAttribute == NSLayoutAttribute.Height)                               //il faut modifier la contrainte de hauteur
                    cst.Constant = viewAcontenth;
            }
        }
cntdetails.AddSubview(cviewA.View);

But it doesn't work as expected, the hiehgt of the main view is increased a bit each time I click the button and one of the view (A or B) in not visible. 但这并没有按预期方式工作,每当我单击该按钮时,主视图的hiehgt都会增加一点,并且其中一个视图(A或B)不可见。

EDIT: cntdetails has equal constraint at top, leading, trailing and height with the main view. 编辑:cntdetails在顶部,前导,尾随和高度与主视图具有相同的约束。 Then I just have to modify the height constraint and actualy this part work. 然后,我只需要修改高度约束,然后实际完成此部分即可。

EDIT: I tried to add constrains to keep the size of A and B following the size of cntdetail but it's not better: 编辑:我试图添加约束以保持cntdetail的大小之后A和B的大小,但这并不更好:

cviewA.View.TranslatesAutoresizingMaskIntoConstraints = false;
cviewA.View.LeadingAnchor.ConstraintEqualTo(cntdetails.LeadingAnchor).Active = true;
cviewA.View.TrailingAnchor.ConstraintEqualTo(cntdetails.TrailingAnchor).Active = true;
cviewA.View.WidthAnchor.ConstraintEqualTo(cntdetails.WidthAnchor).Active = true;
cviewA.View.HeightAnchor.ConstraintEqualTo(cntdetails.HeightAnchor).Active = true;

Finally I found some mistakes I made. 最终,我发现了一些错误。 This page helped For the main view that increased it was because of my function calculating the view height. 此页面有助于增加主要视图,这是因为我的功能计算了视图高度。

First in the viewdidload: 首先在viewdidload中:

cviewA = Storyboard.InstantiateViewController("SaisieVocale");
cviewB = Storyboard.InstantiateViewController("SaisieDetaillee");
//AddChildViewController(cviewA);   no need to do that here
//AddChildViewController(cviewB);
viewAcontenth = GetContentHeight(cviewA.View); //a personal function that calculate the height of the content, it works
viewBcontenth = GetContentHeight(cviewB.View);

When switching from A to B : 从A切换到B时:

//change the height constraint of the uiview to fit the height of my views 
        foreach (var cst in cntdetails.Constraints)
        {
            if ((UIView)cst.FirstItem == cntdetails || (UIView)cst.SecondItem == cntdetails)
            {
                if (cst.FirstAttribute == NSLayoutAttribute.Height)                               //il faut modifier la contrainte de hauteur
                    cst.Constant = viewAcontenth;
            }
        }

//first remove B except the first time
cviewB.DidMoveToParentViewController(null);
cviewB.View.RemoveFromSuperview();
cviewB.RemoveFromParentViewController();

//next add A:
this.AddChildViewController(cviewA);
cviewA.View.Frame= cntdetails.Bounds;
cntdetails.AddSubview(cviewA.View);
cviewA.DidMoveToParentViewController(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM