我想要具有collectionview的视觉美感,但是遇到对齐问题。 我和你分享。 第一次加载页面时没有问题。
当我滚动到收藏夹视图时,会有一些变化。
我想要的是collectionview横向移动并且对齐方式的对齐方式没有改变,但是我已经尽力寻找解决方案。我与您分享代码。
override func awakeFromNib() {
super.awakeFromNib()
self.celloectionCat.delegate = self
self.celloectionCat.dataSource = self
self.celloectionCat?.register(UINib(nibName: "mycell", bundle: nil), forCellWithReuseIdentifier: "mycell")
self.celloectionCat.showsHorizontalScrollIndicator = false
self.celloectionCat.showsVerticalScrollIndicator = false
self.celloectionCat.isPagingEnabled = true;
self.celloectionCat.alwaysBounceVertical = false
}
extension Cell : UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 45.0
let hpadding: CGFloat = 15.0
let collectionViewWidthSize = collectionView.frame.size.width - padding
let collectionViewHeightSize = collectionView.frame.size.height - hpadding
return CGSize(width: collectionViewWidthSize / 2 , height: collectionViewHeightSize / 2 )
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 15
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 15
}
}
答案 0 :(得分:0)
这是因为启用分页后,收集视图将以收集视图大小的倍数停止内容视图。
解决问题的一种方法是将单元格分成多个部分,具体来说,每页一个部分,每个部分最多四个单元格。如果正确设置了部分插图,则一个部分将是集合视图的大小,并且单元格将在该部分的中心对齐。
这是您的代码,稍有更改。我使用了硬编码的数字,但是我建议在您的代码中不要这样做(此外,您应该考虑将像元大小的计算移出sizeForItem方法,因为这将被多次调用,但是大小不会改变)。
@IBOutlet weak var collectionView: UICollectionView! {
didSet {
collectionView.dataSource = self
collectionView.delegate = self
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.isPagingEnabled = true
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell identifier", for: indexPath)
// setup your cell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 45.0
let hpadding: CGFloat = 15.0
let collectionViewWidthSize = collectionView.frame.size.width - padding
let collectionViewHeightSize = collectionView.frame.size.height - hpadding
return CGSize(width: collectionViewWidthSize / 2 , height: collectionViewHeightSize / 2 )
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
{
return 15
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 15
}