【发布时间】:2019-06-17 14:33:51
【问题描述】:
我的场景,我正在尝试根据选择实现UICollectionView 水平scrollview 第一个和最后一个单元格中心对齐。在这里,第一次CollectionViewCell 第一个单元格没有显示确切的中心位置。如果我didselect 一切正常。
我需要修复第一次应用打开第一个单元格时没有显示确切的中心位置。
注意:我没有添加页眉和页脚,我也使用故事板完成了我的设计。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let text = self.items[indexPath.row]
let cellWidth = text.size(withAttributes:[.font: UIFont.systemFont(ofSize: 14.0)]).width + 10.0
return CGSize(width: cellWidth, height: collectionView.bounds.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let inset: CGFloat = collectionView.frame.width * 0.5 - 52 * 0.5
return UIEdgeInsets(top: 0, left: inset, bottom: 0, right: inset)
}
【问题讨论】:
-
不能直接使用52,因为每个单元格的宽度都是动态的。
标签: ios swift uicollectionview