Spacing between collection view cells will be uneven without performing some simple math to calculate the correct size of the cells when including space. In this video, see what code must be added to create perfectly evenly spaced collection view cells.
- [Instructor] Let's look at giving…our collection view cells equal amounts of space.…So we have them uniform sizes,…but we need to space them evenly.…Let's head over into Xcode, and take…a look at how that might work.…So what we'll do is underneath the itemsPerRow constant,…I'll define one called padding.…And I'm going to have this be a CGFloat,…and I'll set it equal to five.…So five points of space, between each row and column.…
Now, padding is something that's…a little bit difficult to figure out,…you have to do some math on it.…The simple way to do it would be to subtract padding…from the width, and then just a minimum interim spacing…to padding, so that's the spacing between items,…and the minimum line spacing to padding.…If you run this, you might notice what's wrong.…The spacing does look even, but watch what happens…when I change the number of items per row…to two for example.…
When we set the number of items per row to two,…there's a little bit more of an obvious…difference in the spacing horizontally.…
- Alert controllers and actions
- Sliders, progress bars, and switches
- Creating UI elements with code
- Displaying web content with Safari view controllers and WKWebView
- Creating collection view controllers
- Creating tabbed apps
- Publishing your app with App Store Connect
Skill Level Beginner
1. Master Views
2. Display Web Content
3. Collection Views
4. Multiview Templates
Customize a table view cell4m 18s
5. Publish an App
Next steps3m 28s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.