I have autosize cell collection view in which i need to show data horizontally. All working fine but i need to put 5px spacing between two cell so minimumLineSpacing
property and problem started My last cell not properly display because of line spacing. Here is the image
Here is the code
extension TopicVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5.0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HastagCell.identifier, for: indexPath) as! HastagCell
let index = indexPath.row % arrClrFollowTopic.count
cell.btnHashTag.backgroundColor = Color.custom(hexString: arrClrFollowTopic[index], alpha: 1.0).value
if indexPath.row % 3 == 0
{
cell.btnHashTag.setTitle(strTitle: "#Testing 123545")
}
else
{
cell.btnHashTag.setTitle(strTitle: "#Testing")
}
return cell;
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.objCollectionView.register(HastagCell.nib, forCellWithReuseIdentifier: HastagCell.identifier)
if let collectionViewLayout = self.objCollectionView.collectionViewLayout as? UICollectionViewFlowLayout{
collectionViewLayout.estimatedItemSize = CGSize.init(width: 1.0, height: 1.0)
}
self.objCollectionView.delegate = self
self.objCollectionView.dataSource = self
// Do any additional setup after loading the view.
}
My Cell design with autolayout
Collectionview layout design
You need to set the minimumInteritemSpacing value of your layout to the same as minimumLineSpacing.
So add :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With