An abstract base class for generating layout information for a collection view.
- iOS 6.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
A layout object determines the placement of cells, supplementary views, and decoration views inside the collection view’s bounds and reports that information to the collection view. The collection view then applies the provided layout information to the corresponding views so that they can be presented onscreen.
You must subclass
UICollection in order to use it. Before you consider subclassing, however, consider whether you can adapt
UICollection to your layout needs.
The layout object defines the position, size, and visual state of items in the collection view, based on the design of the layout. The views for the layout are created by the collection view’s data source.
You lay out three types of visual elements in a collection view:
Cells are the main elements positioned by the layout. Each cell represents a single data item in the collection. You can make cells interactive so that a user can perform actions like selecting, dragging, and reordering the cells. A collection view can have a single group of cells, or you can divide those cells into multiple sections. The layout object arranges the cells in the collection view’s content area.
Supplementary views present data but cannot be selected by the user. You use supplementary views to implement things like header and footer views for a given section or for the entire collection view. Supplementary views are optional and their use and placement is defined by the layout object.
Decoration views are visual adornments, like badges, that cannot be selected and are not inherently tied to the data of the collection view. Decoration views are another type of supplementary view. Like supplementary views, they are optional and their use and placement is defined by the layout object.
The collection view asks its layout object to provide layout information for these elements at many different times. Every cell and view that appears on screen is positioned using information from the layout object. Similarly, every time items are inserted into or deleted from the collection view, an additional layout pass occurs for the items being added or removed. However, the collection view always limits layout to the objects that are visible onscreen.
Methods to Override
Every layout object should implement the following methods:
layout(if your layout supports supplementary views)
Attributes For Supplementary View(of Kind: at:)
layout(if your layout supports decoration views)
Attributes For Decoration View(of Kind: at:)
These methods provide the fundamental layout information that the collection view needs to place contents on the screen. If your layout doesn’t support supplementary or decoration views, don’t implement the corresponding methods.
When the data in the collection view changes and items are to be inserted or deleted, the collection view asks its layout object to update the layout information. Specifically, any item that is moved, added, or deleted must have its layout information updated to reflect its new location. For moved items, the collection view uses the standard methods to retrieve the item’s updated layout attributes. For items being inserted or deleted, the collection view calls some different methods, which you should override to provide the appropriate layout information:
In addition to these methods, you can also override the
prepare(for to handle any layout-related preparation. You can also override the
finalize method and use it to add animations to the overall animation block or to implement any final layout-related tasks.
Optimizing Layout Performance Using Invalidation Contexts
When designing your custom layouts, you can improve performance by invalidating only those parts of your layout that actually changed. When you change items, calling the
invalidate method forces the collection view to recompute all of its layout information and reapply it. A better solution is to recompute only the layout information that changed, which is exactly what invalidation contexts allow you to do. An invalidation context lets you specify which parts of the layout changed. The layout object can then use that information to minimize the amount of data it recomputes.
To define a custom invalidation context for your layout, subclass the
UICollection class. In your subclass, define custom properties that represent the parts of your layout data that can be recomputed independently. When you need to invalidate your layout at runtime, create an instance of your invalidation context subclass, configure the custom properties based on what layout information changed, and pass that object to your layout’s
invalidate method. Your custom implementation of that method can use the information in the invalidation context to recompute only the portions of your layout that changed.
If you define a custom invalidation context class for your layout object, you should also override the
invalidation method and return your custom class. The collection view always creates an instance of the class you specify when it needs an invalidation context. Returning your custom subclass from this method ensures that your layout object always has the invalidation context it expects.