使用约束

时间:2018-01-09 19:03:39

标签: c# ios xamarin.ios

在故事板中,我有3个屏幕(控制器视图): 1)控制器"主要"包含一个名为cntdetail的uiview和一个名为viewswitch的按钮 2)控制器" A"有几个元素(标签,文本字段......) 3)控制器" B"还有几个要素

我需要它在视图出现时以cntdetail加载视图A,并在点击按钮时从A切换到B,反之亦然。

我正在做什么(它在C#中,但几乎像swift):

在主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);

单击按钮时(在viewdid中显示几乎相同)(我使用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);

但它并没有按预期工作,每次点击按钮时,主视图的hiehgt都会增加一点,而其中一个视图(A或B)不可见。

编辑:cntdetails在主视图的顶部,前导,尾随和高度处具有相同的约束。然后我只需修改高度约束并实际执行此部分工作。

编辑:我试图添加约束来保持A和B的大小遵循cntdetail的大小,但它并不是更好:

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;

1 个答案:

答案 0 :(得分:0)

最后我发现了一些错误。 This page helped 对于增加它的主视图是因为我的函数计算视图高度。

首先在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);

从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);