在 UICollectionView 中使用自动布局绘制圆圈的正确时机
Proper Timing to Draw Circles using Autolayout inside UICollectionView
在每个集合视图单元格中,我都有一个视图,我想将其制作成一个完美的圆圈。我使用自动布局将单元格的宽度基于主视图宽度。
我的问题:有些视图没有完美的圆。我猜这是时间上的冲突,在将这些 UIView 变成圆圈之前,我还没有设置主视图边界。我应该在特定时间绘制 cornerRadius 吗?其他一切正常。谢谢!
func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize {
return CGSize(width: self.view.frame.width/3, height: 400)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let sessionCell = (collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.cellID, for: indexPath)) as? DayCollectionViewCell {
sessionCell.circleView.layer.cornerRadius = sessionCell.circleView.frame.width/2
return sessionCell
}
}
这里需要设置
override func layoutSubviews() {
super.layoutSubviews()
self.circleView.layer.cornerRadius = self.circleView.frame.width/2
}
里面DayCollectionViewCell
class
在每个集合视图单元格中,我都有一个视图,我想将其制作成一个完美的圆圈。我使用自动布局将单元格的宽度基于主视图宽度。
我的问题:有些视图没有完美的圆。我猜这是时间上的冲突,在将这些 UIView 变成圆圈之前,我还没有设置主视图边界。我应该在特定时间绘制 cornerRadius 吗?其他一切正常。谢谢!
func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize {
return CGSize(width: self.view.frame.width/3, height: 400)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let sessionCell = (collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.cellID, for: indexPath)) as? DayCollectionViewCell {
sessionCell.circleView.layer.cornerRadius = sessionCell.circleView.frame.width/2
return sessionCell
}
}
这里需要设置
override func layoutSubviews() {
super.layoutSubviews()
self.circleView.layer.cornerRadius = self.circleView.frame.width/2
}
里面DayCollectionViewCell
class