Swift | UICollectionView | Dynamic Cell Width Using Auto Layout | No More Code | Quick Note

The story starts from here, today I was looking into the old codebase and found a bunch of lines of code for calculating the width of `UICollectionViewCell` then I realized is it really required and after Googling I found lots of people are writing code again.

In my scenario, it was breadcrumbs where I needed dynamic width so I removed whole code and here is what I did.

STEP: 1 Added `UILabel` with four constraints.

Screen Shot 2017-08-22 at 10.50.35 AM.png

STEP: 2 Created `UICollectionViewFlowLayout` using `UICollectionView` reference and set the estimated `CGSize`

Screen Shot 2017-08-22 at 11.02.20 AM.png

STEP: 3 set cell `UILabel` to `sizeToFit()`

cell.label.sizeToFit()

That’s All.
Thank you if you want to look into codebase, follow this GIT Link.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s