Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 子容器上的约束_Ios_Swift_View_Constraints_Childviews - Fatal编程技术网

Ios 子容器上的约束

Ios 子容器上的约束,ios,swift,view,constraints,childviews,Ios,Swift,View,Constraints,Childviews,我正在使用view.addChild方法添加子视图 包含的视图显然是350像素。但是,子视图占用了包含视图的所有空间…因此我的想法是强制子视图比其父视图小…但是我的代码不起作用。我可以告诉你,如果我取消注释这两行,它几乎可以工作,但是子视图没有占据我想要的大小,它会阻止其他元素。我就在这里: child.view.translatesAutoresizingMaskIntoConstraints = false let safeArea = view.layoutMarginsGuide

我正在使用view.addChild方法添加子视图

包含的视图显然是350像素。但是,子视图占用了包含视图的所有空间…因此我的想法是强制子视图比其父视图小…但是我的代码不起作用。我可以告诉你,如果我取消注释这两行,它几乎可以工作,但是子视图没有占据我想要的大小,它会阻止其他元素。我就在这里:

  child.view.translatesAutoresizingMaskIntoConstraints = false
  let safeArea = view.layoutMarginsGuide
  //child.view.topAnchor.constraint(equalTo: tableContainer.topAnchor).isActive = true
  // child.view.bottomAnchor.constraint(equalTo: tableContainer.bottomAnchor).isActive = true
        
  child.view.leftAnchor.constraint(equalTo: tableContainer.leftAnchor).isActive = true
  child.view.rightAnchor.constraint(equalTo: tableContainer.rightAnchor).isActive = true
  child.view.heightAnchor.constraint(equalToConstant: 250).isActive = true
        
  self.addChild(child)

让我说得很清楚,我的目标是让子视图达到250像素。谢谢。

您的解决方案可能是这样的

 child.view.translatesAutoresizingMaskIntoConstraints = false
  let safeArea = view.layoutMarginsGuide
  
  //Your left and right anchors tell the compiler exactly how wide the view should be.
  //If you have it set to equal both then the view MUST be exactly the width of parent.      
  child.view.leftAnchor.constraint(equalTo: tableContainer.leftAnchor).isActive = true
  child.view.rightAnchor.constraint(equalTo: tableContainer.rightAnchor).isActive = true

  //Since you're defining your own height here you need to explicitly state where the 
  //view starts at. If you don't define a top, center, bottom or some other constraint
  //it's impossible to know where to put the view.
  child.view.heightAnchor.constraint(equalToConstant: 250).isActive = true
  child.view.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
        
  self.addChild(child)
这样做的原因是,在创建定位时,必须为所有视图设置X&Y定位,即使在逻辑上也是如此。例如,如果定义宽度为100,高度为100,则创建了一个正方形,但该正方形位于何处?在那个例子中没有X或Y。但是,如果使用左锚定和右锚定定义宽度以匹配父视图,则仅通过左锚定和右锚定即可知道视图的宽度。同样的原理也适用于顶部和底部锚。如果定义顶部和底部锚定,则它将是视图的大小(假定将它们设置为相等),并且它将知道高度


在您的实例中,您定义了高度250,但它不知道从何处开始。它是从顶部、中间、底部开始的,它没有线索,因为你没有设置它。IDE具有非常严格的约束,不存在模糊性。

您的解决方案可能类似于此

 child.view.translatesAutoresizingMaskIntoConstraints = false
  let safeArea = view.layoutMarginsGuide
  
  //Your left and right anchors tell the compiler exactly how wide the view should be.
  //If you have it set to equal both then the view MUST be exactly the width of parent.      
  child.view.leftAnchor.constraint(equalTo: tableContainer.leftAnchor).isActive = true
  child.view.rightAnchor.constraint(equalTo: tableContainer.rightAnchor).isActive = true

  //Since you're defining your own height here you need to explicitly state where the 
  //view starts at. If you don't define a top, center, bottom or some other constraint
  //it's impossible to know where to put the view.
  child.view.heightAnchor.constraint(equalToConstant: 250).isActive = true
  child.view.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
        
  self.addChild(child)
这样做的原因是,在创建定位时,必须为所有视图设置X&Y定位,即使在逻辑上也是如此。例如,如果定义宽度为100,高度为100,则创建了一个正方形,但该正方形位于何处?在那个例子中没有X或Y。但是,如果使用左锚定和右锚定定义宽度以匹配父视图,则仅通过左锚定和右锚定即可知道视图的宽度。同样的原理也适用于顶部和底部锚。如果定义顶部和底部锚定,则它将是视图的大小(假定将它们设置为相等),并且它将知道高度


在您的实例中,您定义了高度250,但它不知道从何处开始。它是从顶部、中间、底部开始的,它没有线索,因为你没有设置它。IDE具有非常严格的文字约束,不存在模糊性。

如果要添加“heighAnchor”,请不要添加顶部和底部锚定。只需添加上锚、下锚或将其居中即可。如果添加顶部和底部,则可能会覆盖现有高度。可以设置顶部和底部锚定或其中一个以及高度,但不能同时设置这三个。设置顶部和底部意味着高度。也许您想设置中心和高度?此外,除非您需要RTL中的特定布局,否则您应该使用前导/尾随而不是左/右locales@Paulw11我认为他是在研究身高,他没有明确定义,但基于他所指的身高的限制条件。宽度也是一样的原则。是的,我刚刚意识到并更新了我的comment@xTwisteDx正如您可以清楚地看到的,topanchor和bottom anchor都被注释掉了。所以我的问题仍然是,如果你要添加一个“heighAnchor”,不要添加一个顶部和底部锚。只需添加上锚、下锚或将其居中即可。如果添加顶部和底部,则可能会覆盖现有高度。可以设置顶部和底部锚定或其中一个以及高度,但不能同时设置这三个。设置顶部和底部意味着高度。也许您想设置中心和高度?此外,除非您需要RTL中的特定布局,否则您应该使用前导/尾随而不是左/右locales@Paulw11我认为他是在研究身高,他没有明确定义,但基于他所指的身高的限制条件。宽度也是一样的原则。是的,我刚刚意识到并更新了我的comment@xTwisteDx正如您可以清楚地看到的,topanchor和bottom anchor都被注释掉了。所以我的问题仍然是一样的。试过了,子视图仍然占据了350像素。@coderq你用这个项目做过任何自动布局吗?是否可能存在冲突约束,而不是在代码中?根据您提供的信息,此解决方案应能正常工作。尝试过后,子视图仍占据350像素。@coderq您使用此项目进行过任何自动布局吗?是否可能存在冲突约束,而不是在代码中?此解决方案应基于您提供的信息工作。