Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autosizing collectionview cell not proper display with minimum line space

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 enter image description here

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

enter image description here

Collectionview layout design

enter image description here

like image 357
chirag shah Avatar asked Sep 01 '25 10:09

chirag shah


1 Answers

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
}
like image 192
Anthony Avatar answered Sep 03 '25 23:09

Anthony