更新子视图的布局

时间:2019-03-30 00:47:01

标签: swift layout subview programmatically-created

我遇到这个问题已经很长时间了,但似乎找不到有效的答案。我以编程方式设置了所有内容。

例如,在这里,我将容器视图添加到“集合视图”单元格中。

 addSubviewWithConstraints(subview: containerView, 
     topAnchor: self.topAnchor, topConstant: 100, 
     leadingAnchor: self.leadingAnchor, leadingConstant: 50, 
     trailingAnchor: self.trailingAnchor, trailingConstant: -50, 
     bottomAnchor: self.bottomAnchor, bottomConstant: -100)

 containerView.addSubviewWithConstraints(subview: headerLabel,
     centerXAnchor: self.centerXAnchor, 
     centerYAnchor: self.centerYAnchor, 
     width: containerView.frame.width, 
     height: 44)

当我引用另一个子视图的frame.width时(如在headerLabel约束中所做的那样),它返回0,因此标头标签不可见。如果我改为硬编码宽度值,则可以使用。

通常会在以下情况发生:将超级视图初始化为CGSize.zero,然后在以后更改大小,但由于某种原因,即使在未更改超级视图大小的情况下,在此集合视图单元格中也是如此。

0 个答案:

没有答案
相关问题