Skip to main content

Message List

The ChatMessageListVC is a component that renders a list of messages. It decides how to render a message based on its type and content. The messages data should be provided through the data source named ChatMessageListVCDataSource and some important actions should be delegated through the ChatMessageListVCDelegate, very similar to how the native UITableView and UICollectionView works.


The Stream SDK already provides a ChatChannelVC and a ChatThreadVC that use the ChatMessageListVC to render the messages from a Channel and Thread, respectively. Both components are a full-featured Chat view since both include the message list to render the messages, and the composer to create new messages.


If the built-in ChatChannelVC and ChatThreadVC components do not suit your need, you can use the ChatMessageListVC on your custom views.

In order to properly configure the ChatMessageListVC these are the required dependencies:

  • client: ChatClient, the Stream Chat client instance.
  • dataSource: ChatMessageListVCDataSource, the data source for the ChatMessageListVC. The data source is responsible for providing the messages to be rendered, these messages can be provided by a Channel or a Thread, for example.
  • delegate: ChatMessageListVCDelegate, the delegate for the ChatMessageListVC. The delegate is responsible for handling the actions that are triggered by the user when interacting with the message list.

To add the ChatMessageListVC to your view, you need to add it as a child view controller:

open class CustomChannelViewController: UIViewController, ThemeProvider {

/// Controller for observing data changes within the channel.
open var channelController: ChatChannelController!

/// The message list component responsible to render the messages.
open lazy var messageListVC: ChatMessageListVC = ChatMessageListVC()

/// Controller that handles the composer view.
open lazy var messageComposerVC = ComposerVC()

override open func viewDidLoad() {

// Setup
messageListVC.delegate = self
messageListVC.dataSource = self
messageListVC.client = ChatClient.shared

// Setup Channel Controller
channelController.delegate = self

// Layout
messageListVC.view.translatesAutoresizingMaskIntoConstraints = false
addChildViewController(messageListVC, targetView: view)
messageListVC.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
messageListVC.view.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
messageListVC.view.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
messageListVC.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)

In order to be easier to set up child view controllers you can add this extension to your application:

extension UIViewController {
func addChildViewController(_ child: UIViewController, targetView superview: UIView) {
child.willMove(toParent: self)
child.didMove(toParent: self)

For simplicity, the code above doesn't describe how to set up the message composer, in case you don't have your own message composer and want to set up the one from Stream, you can read the Message Composer documentation.

After adding the message list as a child view controller and configuring its dependencies we need to implement the ChatMessageListVCDataSource to connect the messages from the ChannelController to the ChatMessageListVC. In this case, we are using a ChannelController since we are interested in showing the messages of a channel, but a MessageController could also be used to display the replies of a message thread.

extension ChannelViewController: ChatMessageListVCDataSource {
open func channel(for vc: ChatMessageListVC) -> ChatChannel? {

open func numberOfMessages(in vc: ChatMessageListVC) -> Int {

open func chatMessageListVC(_ vc: ChatMessageListVC, messageAt indexPath: IndexPath) -> ChatMessage? {
return channelController.messages[indexPath.item]

open func chatMessageListVC(
_ vc: ChatMessageListVC,
messageLayoutOptionsAt indexPath: IndexPath
) -> ChatMessageLayoutOptions {
guard let channel = else { return [] }
return messageListVC.components.messageLayoutOptionsResolver.optionsForMessage(
at: indexPath,
in: channel,
with: AnyRandomAccessCollection(channelController.messages),
appearance: messageListVC.appearance

Next, we need to implement the ChatMessageListVCDelegate to handle the actions that are triggered by the user when interacting with the message list.

extension ChannelViewController: ChatMessageListVCDelegate {
open func chatMessageListVC(
_ vc: ChatMessageListVC,
willDisplayMessageAt indexPath: IndexPath
) {

// Load previous messages before showing the last 10 messages.
if indexPath.row < channelController.messages.count - 10 {


open func chatMessageListVC(
_ vc: ChatMessageListVC,
didTapOnAction actionItem: ChatMessageActionItem,
for message: ChatMessage
) {
// Handle message actions
switch actionItem {
case is EditActionItem:
dismiss(animated: true) { [weak self] in
case is InlineReplyActionItem:
dismiss(animated: true) { [weak self] in
case is ThreadReplyActionItem:
dismiss(animated: true) { [weak self] in

open func chatMessageListVC(_ vc: ChatMessageListVC, scrollViewDidScroll scrollView: UIScrollView) {
// Handle scroll events, and check if the last message was read, to mark the channel read.
if messageListVC.listView.isLastCellFullyVisible, == true {


Currently, by implementing the ChatMessageListVCDelegate we are able to handle when a user performs an action on a message, when a message will be displayed, and when the user is scrolling the message list. More events might be added in the future, but for now, these should be enough to implement the most common features in a chat view, like pagination, marking the channel as read when the user scrolls to the bottom, and handling message actions.

Finally, we need to implement the ChannelControllerDelegate to handle the events from the ChannelController. This will make sure that the messages are always in sync with the server.

extension ChannelViewController: ChatChannelControllerDelegate {

open func channelController(
_ channelController: ChatChannelController,
didUpdateMessages changes: [ListChange<ChatMessage>]
) {
messageListVC.updateMessages(with: changes)

open func channelController(
_ channelController: ChatChannelController,
didUpdateChannel channel: EntityChange<ChatChannel>
) {
let channelUnreadCount = ?? .noUnread
messageListVC.scrollToLatestMessageButton.content = channelUnreadCount

open func channelController(
_ channelController: ChatChannelController,
didChangeTypingUsers typingUsers: Set<ChatUser>
) {
guard channelController.areTypingEventsEnabled else { return }

let currentUserId = channelController.client.currentUserId

let typingUsersWithoutCurrentUser = typingUsers
.sorted { $ < $ }
.filter { $ != currentUserId }

if typingUsersWithoutCurrentUser.isEmpty {
} else {
messageListVC.showTypingIndicator(typingUsers: typingUsersWithoutCurrentUser)

UI Customization

You can customize the message list by subclassing the ChatMessageListVC and replacing the Components.default.messageListVC component.

Components.default.messageListVC = CustomMessageListVC.self

You can find more information on how the components configuration works here.

Message Content View

In order to change how the messages are rendered, you need to subclass the ChatMessageContentView and replace it in the Components.default.messageContentView. For more details on how you can customize the message content view, you can take a look at the Customizing Messages documentation.

You can also set your custom ChatMessageContentView in the ChatMessageListVC.cellContentClassForMessage() function, this is especially useful if you have multiple instances of ChatMessageListVC and each have different ChatMessageContentView's.

final class CustomMessageListVC: ChatMessageListVC {

override func cellContentClassForMessage(at indexPath: IndexPath) -> ChatMessageContentView.Type {


As you can see above, by overriding the cellContentClassForMessage(at:) function we can change the ChatMessageContentView that is used to render the message.

Message List Layout

Like any other component in the SDK, you can customize the message list layout by overriding the setUpLayout() lifecycle method when subclassing ChatMessageListVC.

final class CustomMessageListVC: ChatMessageListVC {
override func setUpLayout() {

scrollToLatestMessageButton.centerXAnchor.constraint(equalTo: view.layoutMarginsGuide.centerXAnchor)


In the simple example above, we moved the scrollToLatestMessageButton to the center bottom of the message list, instead of the bottom right corner, and also removed the dateOverlayView from the view hierarchy.

The message list uses the ChatMessageListRouter navigation component to handle the routing, like for example showing threads and attachment previews, as well as the popup actions view. You can customize the navigation by providing your own.

Components.default.messageListRouter = CustomChatMessageListRouter()

You can find more information on how the components configuration works here.



The object that acts as the data source of the message list.

public weak var dataSource: ChatMessageListVCDataSource?


The object that acts as the delegate of the message list.

public weak var delegate: ChatMessageListVCDelegate?


The root object representing the Stream Chat.

public var client: ChatClient!


The router object that handles navigation to other view controllers.

open lazy var router: ChatMessageListRouter 


A View used to display the messages

open private(set) lazy var listView: ChatMessageListView 


A View used to display date of currently displayed messages

open private(set) lazy var dateOverlayView: ChatMessageListScrollOverlayView 


A View which displays information about current users who are typing.

open private(set) lazy var typingIndicatorView: TypingIndicatorView = components


The height of the typing indicator view

open private(set) var typingIndicatorViewHeight: CGFloat = 28


A Boolean value indicating whether the typing events are enabled.

open var isTypingEventsEnabled: Bool 


A button to scroll the collection view to the bottom. Visible when there is unread message and the collection view is not at the bottom already.

open private(set) lazy var scrollToLatestMessageButton: ScrollToLatestMessageButton = components


open var isScrollToBottomButtonVisible: Bool 



override open func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) 


override open func setUp() 


override open func setUpLayout() 


override open func setUpAppearance() 


Returns layout options for the message on given indexPath.

open func cellLayoutOptionsForMessage(at indexPath: IndexPath) -> ChatMessageLayoutOptions 

Layout options are used to determine the layout of the message. By default there is one message with all possible layout and layout options determines which parts of the message are visible for the given message.


Returns the content view class for the message at given indexPath

open func cellContentClassForMessage(at indexPath: IndexPath) -> ChatMessageContentView.Type 


Returns the attachment view injector for the message at given indexPath

open func attachmentViewInjectorClassForMessage(at indexPath: IndexPath) -> AttachmentViewInjector.Type? 


Set the visibility of scrollToLatestMessageButton.

open func setScrollToLatestMessageButton(visible: Bool, animated: Bool = true) 


Action for scrollToLatestMessageButton that scroll to most recent message.

@objc open func scrollToLatestMessage() 


Scrolls to most recent message

open func scrollToMostRecentMessage(animated: Bool = true) 


Updates the collection view data with given changes.

open func updateMessages(with changes: [ListChange<ChatMessage>], completion: (() -> Void)? = nil) 


Handles tap action on the table view.

@objc open func handleTap(_ gesture: UITapGestureRecognizer) 

Default implementation will dismiss the keyboard if it is open


Handles long press action on collection view.

@objc open func handleLongPress(_ gesture: UILongPressGestureRecognizer) 

Default implementation will convert the gesture location to collection view's indexPath and then call selection action on the selected cell.


The message cell was select and should show the available message actions.

open func didSelectMessageCell(at indexPath: IndexPath) 


  • indexPath: The index path that the message was selected.


Opens thread detail for given MessageId.

open func showThread(messageId: MessageId) 


Shows typing Indicator.

open func showTypingIndicator(typingUsers: [ChatUser]) 


  • typingUsers: typing users gotten from channelController


Hides typing Indicator.

open func hideTypingIndicator() 


open func numberOfSections(in tableView: UITableView) -> Int 


open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 


open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 


open func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 


open func scrollViewDidScroll(_ scrollView: UIScrollView) 


open func scrollOverlay(
_ overlay: ChatMessageListScrollOverlayView,
textForItemAt indexPath: IndexPath
) -> String?


open func chatMessageActionsVC(
_ vc: ChatMessageActionsVC,
message: ChatMessage,
didTapOnActionItem actionItem: ChatMessageActionItem


open func chatMessageActionsVCDidFinish(_ vc: ChatMessageActionsVC) 


open func messageContentViewDidTapOnErrorIndicator(_ indexPath: IndexPath?) 


open func messageContentViewDidTapOnThread(_ indexPath: IndexPath?) 


open func messageContentViewDidTapOnQuotedMessage(_ indexPath: IndexPath?) 


open func messageContentViewDidTapOnAvatarView(_ indexPath: IndexPath?) 


open func galleryMessageContentView(
at indexPath: IndexPath?,
didTapAttachmentPreview attachmentId: AttachmentId,
previews: [GalleryItemPreview]


open func galleryMessageContentView(
at indexPath: IndexPath?,
didTakeActionOnUploadingAttachment attachmentId: AttachmentId


open func didTapOnLinkAttachment(
_ attachment: ChatMessageLinkAttachment,
at indexPath: IndexPath?


open func didTapOnAttachment(
_ attachment: ChatMessageFileAttachment,
at indexPath: IndexPath?


Executes the provided action on the message

open func didTapOnAttachmentAction(
_ action: AttachmentAction,
at indexPath: IndexPath


open func gestureRecognizer(
_ gestureRecognizer: UIGestureRecognizer,
shouldReceive touch: UITouch
) -> Bool


public func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool

Did you find this page helpful?