如何在 swift 4 中在中心显示 CollectionViewCell [英] How to display CollectionViewCell in center in swift 4
本文介绍了如何在 swift 4 中在中心显示 CollectionViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Collectionview 单元格在一行中有两个项目,但 Collectionview 单元格不在我的设计中.
Collectionview cell has a two item in one row, But Collectionview cell doesn't center in my design.
如何以中心对齐方式显示 collectionViewCell?我想显示如下图
How can I display the collectionViewCell in center alignment? I want to display like the following image
我尝试了很多次,无法显示CollectionView的中心.请帮帮我!
I am trying many times, can't display the center of CollectionView. Please Help me!
推荐答案
工作代码 Swift 4 |斯威夫特 5
你需要像这样使用UICollectionViewDelegateFlowLayout方法
class CardListViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
// UICollectionViewDelegateFlowLayout Delegate method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let leftAndRightPaddings: CGFloat = 45.0
let numberOfItemsPerRow: CGFloat = 2.0
let width = (collectionView.frame.width-leftAndRightPaddings)/numberOfItemsPerRow
return CGSize(width: width, height: width) // You can change width and height here as pr your requirement
}
}
输出:
这篇关于如何在 swift 4 中在中心显示 CollectionViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文