Fix: Multiline Text Input Not Working in SwiftUI TextField

Written by

in

Quick answer: Use TextField with axis: .vertical (iOS 16+) for expanding single-line fields, TextEditor (iOS 14+) for full multiline editing, or wrap UITextView via UIViewRepresentable (iOS 13+) for maximum compatibility and customization.

Why this happens

SwiftUI’s original TextField was designed for single-line input only. Prior to iOS 16, setting lineLimit or multilineTextAlignment on a TextField had no effect — the control simply ignored those modifiers and stayed single-line. Developers had to either wrap UITextView through UIViewRepresentable or wait for Apple to introduce a proper multiline component. The introduction of TextEditor in iOS 14 and the axis parameter in iOS 16 finally provided native solutions, but version requirements and behavioral differences still cause confusion.

How to fix it

1. Use TextField with axis: .vertical (iOS 16+)

This is now the simplest approach for text fields that should start as a single line and grow vertically as the user types. The axis parameter tells the field to lay out its content vertically, and lineLimit with a range reserves minimum space while allowing expansion.

import SwiftUI

struct ExpandingTextField: View {
    @State private var text = ""

    var body: some View {
        TextField("Enter text", text: $text, axis: .vertical)
            .lineLimit(3...8)  // minimum 3 lines, max 8 before scrolling
            .textFieldStyle(.roundedBorder)
            .padding()
    }
}

Key points:
– axis: .vertical enables multiline behavior
– lineLimit(3...8) sets minimum visible lines (3) and maximum before scrolling (8)
– Works with all standard TextField styles and modifiers
– Requires iOS 16 / macOS 13 / watchOS 9 / tvOS 16

2. Use TextEditor for full multiline editing (iOS 14+)

TextEditor is purpose-built for multiline text. It always shows multiple lines and scrolls internally when content exceeds its frame. Ideal for notes, comments, or any long-form input.

import SwiftUI

struct NotesEditor: View {
    @State private var notes = ""

    var body: some View {
        TextEditor(text: $notes)
            .font(.body)
            .padding(8)
            .overlay(
                RoundedRectangle(cornerRadius: 8)
                    .stroke(Color.gray.opacity(0.3), lineWidth: 1)
            )
            .padding()
    }
}

Customization tips:
– Add placeholder text by overlaying a Text view when notes.isEmpty
– Use .scrollContentBackground(.hidden) (iOS 16+) to remove default background
– Apply .focused($focusState) to manage keyboard dismissal

3. Wrap UITextView via UIViewRepresentable (iOS 13+)

When you need to support iOS 13 or require UITextView features not exposed by SwiftUI (custom input accessories, text kit integration, precise cursor control), a wrapper remains the most flexible option.

import SwiftUI

struct MultilineTextView: UIViewRepresentable {
    @Binding var text: String
    var configuration: (UITextView) -> Void = { _ in }

    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.isScrollEnabled = true
        textView.isEditable = true
        textView.font = .preferredFont(forTextStyle: .body)
        textView.delegate = context.coordinator
        return textView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        if uiView.text != text {
            uiView.text = text
        }
        configuration(uiView)
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(text: $text)
    }

    class Coordinator: NSObject, UITextViewDelegate {
        @Binding var text: String

        init(text: Binding<String>) {
            _text = text
        }

        func textViewDidChange(_ textView: UITextView) {
            text = textView.text
        }
    }
}

// Usage
struct ContentView: View {
    @State private var message = ""

    var body: some View {
        MultilineTextView(text: $message) { textView in
            textView.textColor = .label
            textView.backgroundColor = .systemBackground
            textView.textContainerInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
        }
        .frame(minHeight: 120)
        .overlay(RoundedRectangle(cornerRadius: 8).stroke(Color.gray.opacity(0.3)))
        .padding()
    }
}

Advantages:
– Full UITextView API access (inputAccessoryView, textContainer, layoutManager)
– Works on iOS 13+
– Proven stability in production apps
– Can integrate with existing UIKit text processing pipelines

4. Dynamic height TextEditor with ZStack technique (iOS 14+)

If you need TextEditor to grow with content instead of scrolling internally, measure its intrinsic size using a hidden Text replica in a ZStack.

import SwiftUI

struct GrowingTextEditor: View {
    @State private var text = ""
    @State private var editorHeight: CGFloat = 40

    var body: some View {
        ZStack(alignment: .topLeading) {
            // Invisible measuring text
            Text(text.isEmpty ? " " : text)
                .font(.body)
                .padding(8)
                .opacity(0)
                .overlay(
                    GeometryReader { geo in
                        Color.clear
                            .onAppear { editorHeight = geo.size.height }
                            .onChange(of: geo.size.height) { editorHeight = $0 }
                    }
                )

            // Actual editor
            TextEditor(text: $text)
                .font(.body)
                .padding(8)
                .frame(height: max(editorHeight, 40))
                .scrollContentBackground(.hidden) // iOS 16+
        }
        .overlay(RoundedRectangle(cornerRadius: 8).stroke(Color.gray.opacity(0.3)))
        .padding()
    }
}

How to prevent it

  • Target iOS 16+ when possible — TextField(axis: .vertical) covers most expanding-field needs with zero wrapper code.
  • Choose the right component: TextField + axis for chat-style inputs, TextEditor for documents/notes, UITextView wrapper for legacy support or advanced features.
  • Test on device — Simulator keyboard behavior differs from hardware; verify scrolling, autocorrection, and input accessory views.
  • Handle empty state — Provide placeholder UI for TextEditor since it lacks a built-in placeholder property.
  • Avoid fixed heights — Use minHeight or dynamic measurement so content isn’t clipped when Dynamic Type sizes change.

FAQ

Why does my TextField still show one line even with lineLimit(5)?

lineLimit on TextField only works when axis: .vertical is also set (iOS 16+). Without the axis parameter, TextField remains single-line regardless of line limit modifiers.

Can I add a placeholder to TextEditor?

TextEditor has no native placeholder. Overlay a Text view with low opacity when the binding is empty, and hide it on onChange or via a focus state:

ZStack(alignment: .topLeading) {
    if text.isEmpty {
        Text("Enter notes...")
            .foregroundColor(.placeholderText)
            .padding(12)
            .allowsHitTesting(false)
    }
    TextEditor(text: $text)
        .scrollContentBackground(.hidden)
}

How do I dismiss the keyboard for a multiline field?

Attach a toolbar with a Done button using .toolbar (iOS 14+) or the inputAccessoryView on the wrapped UITextView. For TextField/TextEditor, use @FocusState to programmatically resign focus:

@FocusState private var isFocused: Bool

TextField("...", text: $text, axis: .vertical)
    .focused($isFocused)
    .toolbar {
        ToolbarItemGroup(placement: .keyboard) {
            Spacer()
            Button("Done") { isFocused = false }
        }
    }
Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.