> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# New Chat

> Add a New Chat screen to a CometChat iOS UI Kit v6 app: a compose button on the conversations list that opens users and groups, and opens the chat picked.

> [accordion] AI Integration Quick Reference
>
> ```json theme={null}
> {
>   "products": ["CometChatSwiftUI", "CometChatUIKit"],
>   "components": ["CometChatConversations", "CometChatUsers", "CometChatGroups", "CometChatMessageHeader", "CometChatMessageList", "CometChatMessageComposer"],
>   "entry": "CometChatConversations overflowMenu: (SwiftUI) / setOverflowMenu(_: UIBarButtonItem) (UIKit)",
>   "pick": "CometChatUsers onItemClick: (User), CometChatGroups onItemClick: (Group) — a public group is joined before onItemClick",
>   "note": "The Kit has no New Chat screen: you combine Users and Groups behind a segmented control."
> }
> ```

A New Chat screen lets the user start a one-to-one chat or open a group without an existing
conversation. You build it from `CometChatUsers` and `CometChatGroups`, opened from a button
on the conversations list.

## Overview

1. A compose button on the conversations list opens the New Chat screen.
2. A segmented control switches between **Users** and **Groups**; both lists search and page.
3. Picking a user opens a one-to-one chat. Picking a public group you have not joined joins it
   first, then opens it.

## Prerequisites

* The Kit is set up and a user is logged in — see [Getting Started](https://www.cometchat.com/docs/ui-kit/ios/v6/getting-started-swiftui.md).
* A chat screen that takes a `User` or a `Group` — see
  [Message Privately](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-message-privately.md) for one.

## Components

| Component | Role |
| - | - |
| `CometChatConversations` | Hosts the compose button in its bar. |
| `CometChatUsers` | Lists users; a tap hands you the `User`. |
| `CometChatGroups` | Lists groups; a tap on a public group joins it, then hands you the `Group`. |
| `CometChatMessageHeader` / `List` / `Composer` | The chat that opens. |

## Integration Steps

### 1. Add the entry point to the conversations list

> [tab] SwiftUI
>
> ```swift ChatsView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct ChatsView: View {
>     @State private var showsNewChat = false
>
>     var body: some View {
>         CometChatConversations(
>             overflowMenu: {
>                 AnyView(Button { showsNewChat = true } label: {
>                     Image(systemName: "square.and.pencil")
>                 }
>                 .accessibilityLabel("New chat"))
>             },
>             onItemClick: { conversation in print("open", conversation.conversationId ?? "") }
>         )
>         .sheet(isPresented: $showsNewChat) {
>             Text("New Chat") // the screen from step 2
>         }
>     }
> }
> ```

> [tab] UIKit
>
> ```swift ChatsViewController.swift theme={null}
> import UIKit
> import CometChatUIKit
> import CometChatSDK
>
> final class ChatsViewController: CometChatConversations {
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         setOverflowMenu(UIBarButtonItem(
>             image: UIImage(systemName: "square.and.pencil"),
>             primaryAction: UIAction { [weak self] _ in self?.showNewChat() }))
>     }
>
>     private func showNewChat() {
>         let newChat = UIViewController() // the screen from step 2
>         present(UINavigationController(rootViewController: newChat), animated: true)
>     }
> }
> ```

### 2. Build the New Chat screen

Put the two lists behind Users / Groups segments, laid out like the conversations and users lists:

* **The navigation bar keeps the title and the search field.** Each list puts its search in the
  bar it is shown in.
* **The segments are the search field's scope bar**, under the field in the same navigation bar,
  not a view of your own above or over the list. The bar's glass and its soft scroll edge are
  theirs too: the rows scroll under the title, the field and the segments, and fade as they go.
* **No background of your own.** The list is the screen's content and runs under the bar.

The bar is the system's, so it follows the system: Liquid Glass on iOS 26, and the flat bar on
earlier versions and under Reduce Transparency.

> [tab] SwiftUI
>
> `cometChatSearchScopes(_:selection:)` gives the shown list's search field its scope buttons and
> tells you which one is selected. Apply it to whichever list is shown, so the bar keeps the
> segments across the switch.
>
> ```swift NewChatView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> /// What the user picked: a person or a group.
> enum NewChatPick {
>     case user(CometChatSDK.User)
>     case group(CometChatSDK.Group)
> }
>
> struct NewChatView: View {
>     let onPick: (NewChatPick) -> Void
>     @Environment(\.dismiss) private var dismiss
>     /// The selected segment: 0 is Users, 1 is Groups.
>     @State private var tab = 0
>
>     var body: some View {
>         NavigationStack {
>             list
>                 // The segments, as the search field's scope bar.
>                 .cometChatSearchScopes(["Users", "Groups"], selection: $tab)
>                 .navigationTitle("New Chat")
>                 .toolbar {
>                     ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } }
>                 }
>         }
>     }
>
>     /// Each list keeps the navigation bar: titled New Chat, with its search field in it.
>     @ViewBuilder private var list: some View {
>         if tab == 0 {
>             CometChatUsers(embedsNavigationStack: false, title: "New Chat",
>                            onItemClick: { user in pick(.user(user)) })
>         } else {
>             // A public group you have not joined is joined first, under a
>             // "Joining Group" alert, then handed to onItemClick.
>             CometChatGroups(title: "New Chat", embedsNavigationStack: false,
>                             onItemClick: { group in pick(.group(group)) })
>         }
>     }
>
>     private func pick(_ choice: NewChatPick) {
>         dismiss()
>         onPick(choice)
>     }
> }
> ```

> [tab] UIKit
>
> Each list has a `searchController` of its own. Put the shown list's on your navigation item and
> give its search bar the segments as scope buttons (`scopeButtonTitles`), shown always
> (`scopeBarActivation = .manual`, `showsScopeBar = true`). The list stays its search bar's
> delegate, for the text; a tap on a scope button reaches you as the controller's
> `searchResultsUpdater`.
>
> ```swift NewChatViewController.swift theme={null}
> import UIKit
> import CometChatUIKit
> import CometChatSDK
>
> final class NewChatViewController: UIViewController, UISearchResultsUpdating {
>     /// Called with the user or the group picked, after the screen closes.
>     var onPickUser: ((CometChatSDK.User) -> Void)?
>     var onPickGroup: ((CometChatSDK.Group) -> Void)?
>
>     private let users = CometChatUsers()
>     private let groups = CometChatGroups()
>     private lazy var lists: [CometChatListBase] = [users, groups]
>     /// The selected segment: 0 is Users, 1 is Groups.
>     private var selected = 0
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         title = "New Chat"
>         navigationItem.largeTitleDisplayMode = .never
>         view.backgroundColor = CometChatUIKitTheme.shared.background1
>         navigationItem.leftBarButtonItem = UIBarButtonItem(
>             systemItem: .cancel, primaryAction: UIAction { [weak self] _ in self?.dismiss(animated: true) })
>
>         users.set(onItemClick: { [weak self] user, _ in
>             self?.dismiss(animated: true) { self?.onPickUser?(user) }
>         })
>         // A public group you have not joined is joined first, then handed here.
>         groups.set(onItemClick: { [weak self] group, _ in
>             self?.dismiss(animated: true) { self?.onPickGroup?(group) }
>         })
>         // Each list's search field moves into this screen's bar: keep the bar while searching,
>         // and the field under the title while the list scrolls.
>         users.searchController.hidesNavigationBarDuringPresentation = false
>         groups.searchController.hidesNavigationBarDuringPresentation = false
>         navigationItem.preferredSearchBarPlacement = .stacked
>         navigationItem.hidesSearchBarWhenScrolling = false
>         showTab()
>     }
>
>     private func showTab() {
>         let show = lists[selected], hide = lists[1 - selected]
>         hide.willMove(toParent: nil); hide.view.removeFromSuperview(); hide.removeFromParent()
>         // The list full-bleed, under the bar.
>         addChild(show)
>         show.view.frame = view.bounds
>         show.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
>         view.addSubview(show.view)
>         show.didMove(toParent: self)
>         // The segments, as the scope bar of this list's search field.
>         let search = show.searchController
>         search.searchResultsUpdater = self
>         search.scopeBarActivation = .manual
>         search.searchBar.scopeButtonTitles = ["Users", "Groups"]
>         search.searchBar.selectedScopeButtonIndex = selected
>         search.searchBar.showsScopeBar = true
>         // The bar shows this list's search and tracks its table.
>         navigationItem.searchController = search
>         setContentScrollView(show.tableView, for: .top)
>     }
>
>     /// `UISearchResultsUpdating`: called for a scope tap, and for every edit of the text. Only a
>     /// new scope changes the list.
>     func updateSearchResults(for searchController: UISearchController) {
>         let index = searchController.searchBar.selectedScopeButtonIndex
>         guard searchController === lists[selected].searchController, index != selected else { return }
>         selected = index
>         showTab()
>     }
> }
> ```

### 3. Handle the selection and open the chat

Open the same chat screen your conversations list opens, with the `User` or the `Group`:

> [tab] SwiftUI
>
> ```swift theme={null}
> struct NewChatHost: View {
>     @State private var showsNewChat = false
>     @State private var user: CometChatSDK.User?
>     @State private var group: CometChatSDK.Group?
>     @State private var showsChat = false
>
>     var body: some View {
>         Button("New chat") { showsNewChat = true }
>             .sheet(isPresented: $showsNewChat) {
>                 NewChatView(onPick: { pick in
>                     switch pick {
>                     case .user(let picked): user = picked; group = nil
>                     case .group(let picked): group = picked; user = nil
>                     }
>                     showsChat = true
>                 })
>             }
>             .navigationDestination(isPresented: $showsChat) {
>                 MessageView(user: user, group: group)   // your chat screen
>             }
>     }
> }
>
> enum NewChatPick {
>     case user(CometChatSDK.User)
>     case group(CometChatSDK.Group)
> }
>
> struct NewChatView: View {
>     let onPick: (NewChatPick) -> Void
>     var body: some View { EmptyView() }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let newChat = NewChatViewController()
> newChat.onPickUser = { user in
>     let header = CometChatMessageHeader(), list = CometChatMessageList(), composer = CometChatMessageComposer()
>     header.set(user: user); list.set(user: user); composer.set(user: user)
>     // … push your chat screen holding the three …
> }
> newChat.onPickGroup = { group in
>     print("open the group chat for", group.guid)
> }
>
> final class NewChatViewController: UIViewController {
>     var onPickUser: ((CometChatSDK.User) -> Void)?
>     var onPickGroup: ((CometChatSDK.Group) -> Void)?
> }
> ```

`MessageView` is the chat screen from [Conversation List + Message View](https://www.cometchat.com/docs/ui-kit/ios/v6/conversation-message-view.md#step-2-set-up-the-message-screen), which hosts the header and the composer over the list.

## Implementation Flow

1. The compose button presents the New Chat screen.
2. The user switches between Users and Groups, searching either.
3. A tap closes the screen and hands you the pick.
4. You open the chat with that user or group.

## Customization Options

* **Filter who is listed** — `usersRequestBuilder:` / `set(userRequestBuilder:)` and
  `groupsRequestBuilder:` / `set(groupsRequestBuilder:)`, for example only friends or only
  joined groups.
* **Rows and states** — both lists take view slots and state views; see
  [Users](https://www.cometchat.com/docs/ui-kit/ios/v6/users.md) and [Groups](https://www.cometchat.com/docs/ui-kit/ios/v6/groups.md).

## Edge Cases

| Case | Behaviour |
| - | - |
| A public group you have not joined | Joined under a "Joining Group" alert, then handed to `onItemClick`. A failed join calls `onError`. |
| A password group you have not joined | Handed to `onItemClick` unjoined: check `hasJoined`, ask for the password and join — see [Group Chat](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-group-chat.md). |
| A user with no conversation yet | The chat opens empty; it shows in Conversations once a message is sent. |

## Feature Matrix

| Feature | SwiftUI | UIKit |
| - | - | - |
| Compose button | `overflowMenu:` | `setOverflowMenu(_:)` |
| Users tab | `CometChatUsers(onItemClick:)` | `CometChatUsers()`, `set(onItemClick:)` |
| Groups tab | `CometChatGroups(onItemClick:)` | `CometChatGroups()`, `set(onItemClick:)` |
| Embed in your own stack | `embedsNavigationStack: false` | child view controllers |
| Segments in the navigation bar | `cometChatSearchScopes(_:selection:)` | the search bar's `scopeButtonTitles`, `scopeBarActivation = .manual`, `showsScopeBar`, `searchResultsUpdater` |

## Next Steps

> [card] [Users](https://www.cometchat.com/docs/ui-kit/ios/v6/users.md)
>
> The users list in full

> [card] [Groups](https://www.cometchat.com/docs/ui-kit/ios/v6/groups.md)
>
> The groups list in full

> [card] [Group Chat](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-group-chat.md)
>
> Create, join and manage groups

> [card] [All Guides](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-overview.md)
>
> Browse the other guides


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.