【发布时间】:2019-04-22 03:01:12
【问题描述】:
Xcode 10.2、Swift 5。
我在.xib 文件中定义了一个UICollectionViewCell。我在我的UIViewController(不是UICollectionViewController)子类中加载它并在cellForItemAt中返回它。
在我的UICollectionViewCell(称为TagCell)中,我这样做:
override
func
awakeFromNib()
{
super.awakeFromNib()
self.contentView.translatesAutoresizingMaskIntoConstraints = false
self.translatesAutoresizingMaskIntoConstraints = false
self.layer.borderColor = UIColor(red: 0.354, green: 0.442, blue: 0.297, alpha: 0.74).cgColor
self.layer.borderWidth = 1.0
self.layer.cornerRadius = 4.0
}
笔尖只有我放入的顶级UICollectionViewCell,和一个UILabel,对所有四个边缘都有固定的4像素约束。我在cellForItemAt 中设置了UILabel 的文本。但是,生成的集合会以我在viewDidLoad() 中设置的估计高度呈现所有单元格:
override
func
viewDidLoad()
{
super.viewDidLoad()
let nib = UINib(nibName: "TagCell", bundle: Bundle(for: type(of: self)))
self.collectionView.register(nib, forCellWithReuseIdentifier: "TagCell")
let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.estimatedItemSize = CGSize(width: 60.0, height: 25.0)
}
细胞笔尖文件:
这似乎是我在网上找到的 Apple 和其他资源告诉我要做的任何事情,但我一定错过了一些重要的事情。我还尝试在我的 UILabel 上将水平压缩阻力优先级设置为 1000,但它们一直被截断。
我也尝试过设置>= 宽度约束,但这似乎被忽略了。
在我设置self.contentView.translatesAutoresizingMaskIntoConstraints 之前,我会在调试器中收到冲突约束警告。但现在它不再抱怨>= 宽度约束,甚至认为它违反了它。
【问题讨论】:
-
你能添加故事板图片吗
-
@pooja 我发布了单元格笔尖,我认为这很有用,对吧? UICollectionView 只是我的 UIViewController 主视图中的一个视图。
标签: ios swift uicollectionview constraints