TechnologiesSwiftUI

SubmitLabel struct

iOSmacOStvOSwatchOSvisionOSiOS 15.0+✓ renders

A semantic label describing the label of submission within a view hierarchy.

How it works

A SubmitLabel is a semantic description of the action a text-entry control performs when the user submits its contents, which SwiftUI uses to choose the title of the keyboard's return key. Rather than always showing a generic "return" key, you give the system a hint about intent — searching, sending, joining, going — so the on-screen keyboard presents a matching, localized action key. Reach for SubmitLabel whenever a TextField or SecureField commits to something more specific than a newline, so the keyboard's prominent submit key reads correctly for the task at hand.

  1. Apply a label with the submitLabel(_:) modifier

    You don't construct a SubmitLabel directly on the view; you attach one to a text input through the submitLabel(_:) modifier, which carries the hint down to the keyboard. In the example, .submitLabel(.search) is attached to the "Search" TextField and .submitLabel(.send) to the "Message" field, so each field's return key reflects its own purpose.

  2. Pick a case that names the action

    SubmitLabel is a set of predefined values — among them .search, .send, .done, .go, .join, .next, .return, .route, and .continue — each mapping to a standard return-key title. The example uses .search to mark a query field and .send to mark a message field; swapping in a different case changes which word the keyboard renders without touching any other code.

  3. Pair it with the onSubmit action

    The label only describes the action; it does not perform it. To actually run work when the user taps that key, add an onSubmit(of:_:) modifier whose closure handles the committed text. The example's query and message state hold the entered values, and an onSubmit handler is where you would react to either field being submitted.

  4. Let it scope and propagate through the view tree

    Because submitLabel(_:) is a regular view modifier, a label set on a container applies to every submittable control within it unless an inner control overrides it. Here each TextField inside the Form sets its own label individually, but you could instead label the whole Form once and let both fields inherit the same submit key.

Try it — Change .submitLabel(.send) on the "Message" field to .submitLabel(.done) and watch the keyboard's blue return key relabel from "send" to "done".

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

SubmitLabel.swift
struct SubmitLabelDemo: View {
    @State private var query = ""
    @State private var message = ""
    var body: some View {
        Form {
            TextField("Search", text: $query)
                .submitLabel(.search)
            TextField("Message", text: $message)
                .submitLabel(.send)
        }
        .padding()
    }
}
Live preview
Search Message
Search Message
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →