首页 > 解决方案 > 子视图的底部锚点相对于父视图的顶部锚点,而不是预期的底部锚点

问题描述

我有一个 UICollectionView,在每个 UICollectionViewCell 中我都有一个应该位于单元格底部的 UILabel。除了将 UILabel 相对于单元格底部定位的约束(假设标签在单元格底部上方 40 点)之外,所有约束都正常工作。相反,我的代码将其放置在单元格顶部下方40 点处,因此 UILabel 位于单元格顶部,而不是底部。代码清楚地为单元格的 contentView 指定了底部锚点,所以我不确定发生了什么。

// Constraints work correctly
self.text.widthAnchor.constraint(equalToConstant: CellConstants.textWidthAnchor).isActive = true
self.text.heightAnchor.constraint(equalToConstant: CellConstants.textHeightAnchor).isActive = true
self.text.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: CellConstants.textLeftAnchor).isActive = true
// Incorrect constraint 
self.text.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 40).isActive = true

标签: iosswiftautolayout

解决方案


推荐阅读