答案 0 :(得分:2)
您需要使用collectionView flowLayout
let width = UIScreen.main.bounds.size.width
//MARK:- Collection View Flow Layouts
extension HomeVC : UICollectionViewDelegateFlowLayout
{
//MARK: Setting size of cell
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: width/3-16, height: width/3-16)
}
//MARK: Setting space Around Corners of Cells
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsetsMake(8, 8, 8, 8)
}
//MARK: Setting space between two sections
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
{
return 0
}
//MARK: Setting Space between two Cels
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
return 8
}
}
StoryBoard Design
输出:
答案 1 :(得分:0)
你也可以这样使用:
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
let width = UIScreen.main.bounds.size.width
layout.itemSize = CGSize(width: width/3.05, height: width/3.05)
layout.sectionInset = UIEdgeInsets(top: 2, left: 1, bottom: 2, right: 1)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 2
collectionView!.collectionViewLayout = layout
collectionView.reloadData()
<强>输出:强>
希望这有帮助。