Chat Messaging

Chat Messaging

by dockui

A clean chat interface built with SwiftUI. It features message bubbles, timestamps, a scrollable message feed, and a modern input bar with send button.

Platform
SwiftUI
Code
Native Swift
Format
Screen
Access
Free

Code Snippet

import SwiftUI

struct ChatMessage: Identifiable {
    let id = UUID()
    let text: String
    let isCurrentUser: Bool
    let timestamp: String
}

struct ChatMessagingTemplate: View {
    @State private var messages: [ChatMessage] = [
        ChatMessage(text: "Hey! đź‘‹", isCurrentUser: false, timestamp: "09:00"),
        ChatMessage(text: "Hi! How are you?", isCurrentUser: true, timestamp: "09:01"),
        ChatMessage(text: "I'm good, thanks! Working on the new app design.", isCurrentUser: false, timestamp: "09:02"),
        ChatMessage(text: "That sounds awesome! Need any help?", isCurrentUser: true, timestamp: "09:03"),
        ChatMessage(text: "Maybe later, thanks!", isCurrentUser: false, timestamp: "09:04")
    ]
    @State private var inputText: String = ""
    
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                ScrollViewReader { scrollProxy in
                    ScrollView {
                        VStack(spacing: 12) {
                            ForEach(messages) { message in
                                ChatBubble(message: message)
                                    .id(message.id)
                            }
                        }
                        .padding(.vertical)
                        .padding(.horizontal, 8)
                    }
                    .background(Color(.systemGroupedBackground))
                    .onAppear {
                        // Scroll to last message
                        if let last = messages.last {
                            scrollProxy.scrollTo(last.id, anchor: .bottom)
                        }
                    }
                }
                Divider()
                ChatInputBar(inputText: $inputText)
                    .padding(.bottom, 8)
            }
            .navigationTitle("Chat")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

struct ChatBubble: View {
    let message: ChatMessage
    
    var body: some View {
        HStack {
            if message.isCurrentUser { Spacer() }
            VStack(alignment: message.isCurrentUser ? .trailing : .leading, spacing: 4) {
                Text(message.text)
                    .padding(.horizontal, 16)
                    .padding(.vertical, 10)
                    .background(message.isCurrentUser ? Color.blue : Color(.systemGray5))
                    .foregroundColor(message.isCurrentUser ? .white : .primary)
                    .cornerRadius(18)
                Text(message.timestamp)
                    .font(.caption2)
                    .foregroundColor(.secondary)
            }
            if !message.isCurrentUser { Spacer() }
        }
        .padding(.horizontal, 8)
    }
}

struct ChatInputBar: View {
    @Binding var inputText: String
    
    var body: some View {
        HStack(spacing: 8) {
            TextField("Message", text: $inputText)
                .padding(10)
                .background(Color(.systemGray6))
                .cornerRadius(16)
            Button(action: {}) {
                Image(systemName: "paperplane.fill")
                    .foregroundColor(.blue)
                    .font(.title2)
            }
            .disabled(inputText.isEmpty)
        }
        .padding(.horizontal)
    }
}

Similar Blogs

View All Articles
How to Translate Xcode String Catalogs with AI

How to Translate Xcode String Catalogs with AI

Learn how to efficiently translate Xcode string catalogs using AI fast, streamlining localization for your iOS and SwiftUI applications.

by dockui
May 4
How to optimize your SwiftUI app in 2026

How to optimize your SwiftUI app in 2026

Discover top strategies to optimize your SwiftUI app in 2026, enhancing performance, responsiveness, and maintainability with modern concurrency.

by dockui
Apr 29
SwiftUI: iOS Confirmation Alert (Confirm + Cancel)

SwiftUI: iOS Confirmation Alert (Confirm + Cancel)

SwiftUI iOS confirmation alert with Confirm and Cancel buttons. Beginners guide to building safe actions, destructive deletes, and commit dialogs.

by dockui
Nov 11
SwiftUI: IOS Haptic Feedback Guide 2026

SwiftUI: IOS Haptic Feedback Guide 2026

Add click to vibrate haptic feedback in SwiftUI. Full guide to UIImpactFeedbackgenerator, success, error, selection, and tap vibration patterns with code.

by dockui
Nov 11

Code copied to clipboard!