CollectionViewChatLayout

open class CollectionViewChatLayout : UICollectionViewLayout

A collection view layout designed to display items in a grid similar to UITableView, while aligning them to the leading or trailing edge of the UICollectionView. This layout facilitates chat-like behavior by maintaining a constant content offset from the bottom. Additionally, it is capable of handling autosizing cells and supplementary views.

Custom Properties:

CollectionViewChatLayout.delegate

CollectionViewChatLayout.settings

CollectionViewChatLayout.keepContentOffsetAtBottomOnBatchUpdates

CollectionViewChatLayout.processOnlyVisibleItemsOnAnimatedBatchUpdates

CollectionViewChatLayout.visibleBounds

CollectionViewChatLayout.layoutFrame

Custom Methods:

CollectionViewChatLayout.getContentOffsetSnapshot(...)

CollectionViewChatLayout.restoreContentOffset(...)

Custom Properties

  • CollectionViewChatLayout delegate.

    Declaration

    Swift

    open weak var delegate: ChatLayoutDelegate?
  • Additional settings for CollectionViewChatLayout.

    Declaration

    Swift

    public var settings: ChatLayoutSettings { get set }
  • Default UIScrollView behaviour is to keep content offset constant from the top edge. If this flag is set to true CollectionViewChatLayout should try to compensate batch update changes to keep the current content at the bottom of the visible part of UICollectionView.

    NB: Keep in mind that if during the batch content inset changes also (e.g. keyboard frame changes), CollectionViewChatLayout will usually get that information after the animation starts and wont be able to compensate that change too. It should be done manually.

    Declaration

    Swift

    public var keepContentOffsetAtBottomOnBatchUpdates: Bool
  • tries to process only the elements that are currently visible on the screen. But often it is not needed. This flag allows you to have fine control over this behaviour. It set to true by default to keep the compatibility with the older versions of the library.

    NB: This flag is only to provide fine control over the batch updates. If in doubts - keep it true.

    Declaration

    Swift

    public var processOnlyVisibleItemsOnAnimatedBatchUpdates: Bool
  • A mode that enables automatic self-sizing invalidation after Auto Layout changes. It’s advisable to continue using the reload/reconfigure method, especially when multiple changes occur concurrently in an animated fashion. This approach ensures that the CollectionViewChatLayout can handle these changes while maintaining the content offset accurately. Consider using it when no better alternatives are available.

    NB: This is an experimental flag.

    Declaration

    Swift

    @available(iOS 16.0, *)
    public var supportSelfSizingInvalidation: Bool { get set }
  • Represent the currently visible rectangle.

    Declaration

    Swift

    open var visibleBounds: CGRect { get }
  • Represent the rectangle where all the items are aligned.

    Declaration

    Swift

    open var layoutFrame: CGRect { get }

Inherited Properties

  • The direction of the language you used when designing CollectionViewChatLayout layout.

    Declaration

    Swift

    open override var developmentLayoutDirection: UIUserInterfaceLayoutDirection { get }
  • A Boolean value that indicates whether the horizontal coordinate system is automatically flipped at appropriate times.

    Declaration

    Swift

    open override var flipsHorizontallyInOppositeLayoutDirection: Bool { get }
  • Custom layoutAttributesClass is ChatLayoutAttributes.

    Declaration

    Swift

    public override class var layoutAttributesClass: AnyClass { get }
  • Custom invalidationContextClass is ChatLayoutInvalidationContext.

    Declaration

    Swift

    public override class var invalidationContextClass: AnyClass { get }
  • The width and height of the collection view’s contents.

    Declaration

    Swift

    open override var collectionViewContentSize: CGSize { get }
  • There is an issue in IOS 15.1 that proposed content offset is being ignored by the UICollectionView when user is scrolling. This flag enables a hack to compensate this offset later. You can disable it if necessary. Bug reported: https://feedbackassistant.apple.com/feedback/9727104

    PS: This issue was fixed in 15.2

    Declaration

    Swift

    public var enableIOS15_1Fix: Bool

Constructors

  • Default constructor.

    Declaration

    Swift

    public init(flipsHorizontallyInOppositeLayoutDirection: Bool = true)

    Parameters

    flipsHorizontallyInOppositeLayoutDirection

    Indicates whether the horizontal coordinate system is automatically flipped at appropriate times. In practice, this is used to support right-to-left layout.

  • Returns an object initialized from data in a given unarchiver.

    Declaration

    Swift

    public required init?(coder aDecoder: NSCoder)

Custom Methods

Providing Layout Attributes

  • Tells the layout object to update the current layout.

    Declaration

    Swift

    open override func prepare()
  • Retrieves the layout attributes for all of the cells and views in the specified rectangle.

    Declaration

    Swift

    open override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
  • Retrieves layout information for an item at the specified index path with a corresponding cell.

    Declaration

    Swift

    open override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
  • Retrieves the layout attributes for the specified supplementary view.

    Declaration

    Swift

    open override func layoutAttributesForSupplementaryView(ofKind elementKind: String,
                                                            at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?

Coordinating Animated Changes

  • Prepares the layout object for animated changes to the view’s bounds or the insertion or deletion of items.

    Declaration

    Swift

    open override func prepare(forAnimatedBoundsChange oldBounds: CGRect)
  • Cleans up after any animated changes to the view’s bounds or after the insertion or deletion of items.

    Declaration

    Swift

    open override func finalizeAnimatedBoundsChange()

Context Invalidation

  • Asks the layout object if changes to a self-sizing cell require a layout update.

    Declaration

    Swift

    open override func shouldInvalidateLayout(forPreferredLayoutAttributes preferredAttributes: UICollectionViewLayoutAttributes,
                                              withOriginalAttributes originalAttributes: UICollectionViewLayoutAttributes) -> Bool
  • Retrieves a context object that identifies the portions of the layout that should change in response to dynamic cell changes.

    Declaration

    Swift

    open override func invalidationContext(forPreferredLayoutAttributes preferredAttributes: UICollectionViewLayoutAttributes,
                                           withOriginalAttributes originalAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutInvalidationContext
  • Asks the layout object if the new bounds require a layout update.

    Declaration

    Swift

    open override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool
  • Retrieves a context object that defines the portions of the layout that should change when a bounds change occurs.

    Declaration

    Swift

    open override func invalidationContext(forBoundsChange newBounds: CGRect) -> UICollectionViewLayoutInvalidationContext
  • Invalidates the current layout using the information in the provided context object.

    Declaration

    Swift

    open override func invalidateLayout(with context: UICollectionViewLayoutInvalidationContext)
  • Invalidates the current layout and triggers a layout update.

    Declaration

    Swift

    open override func invalidateLayout()
  • Retrieves the content offset to use after an animated layout update or change.

    Declaration

    Swift

    open override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint

Responding to Collection View Updates

  • Notifies the layout object that the contents of the collection view are about to change.

    Declaration

    Swift

    open override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem])
  • Performs any additional animations or clean up needed during a collection view update.

    Declaration

    Swift

    open override func finalizeCollectionViewUpdates()

Cell Appearance Animation

  • Retrieves the starting layout information for an item being inserted into the collection view.

    Declaration

    Swift

    open override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?
  • Retrieves the final layout information for an item that is about to be removed from the collection view.

    Declaration

    Swift

    open override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?

Supplementary View Appearance Animation

  • Retrieves the starting layout information for a supplementary view being inserted into the collection view.

    Declaration

    Swift

    open override func initialLayoutAttributesForAppearingSupplementaryElement(ofKind elementKind: String,
                                                                               at elementIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?
  • Retrieves the final layout information for a supplementary view that is about to be removed from the collection view.

    Declaration

    Swift

    open override func finalLayoutAttributesForDisappearingSupplementaryElement(ofKind elementKind: String,
                                                                                at elementIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?