TechnologiesSwiftUI

RenameButton struct

iOSmacOStvOSwatchOSvisionOSiOS 16.0+✓ renders

A button that triggers a standard rename action.

How it works

RenameButton is a semantic control that presents the system's standard rename affordance, using the platform's localized label and icon so users encounter a familiar action across your app. Rather than wiring up your own button with custom title and styling, you place a RenameButton where renaming makes sense — typically inside a menu, context menu, or toolbar — and let SwiftUI supply the correct presentation. The button has no closure of its own; it forwards its intent to whatever rename behavior the surrounding view has declared. Reach for it whenever a view's content can be renamed and you want that gesture to look and read like the rest of the system.

  1. Place RenameButton where the action belongs

    RenameButton() is initialized with no arguments — it carries the standard rename title and image for you. Drop it into a container that surfaces actions, such as the Menu("Options") in the example, alongside your other entries like the Button("Duplicate").

  2. Supply the behavior with renameAction(_:)

    A RenameButton does nothing on its own; it triggers the rename action registered on an enclosing view. Attach .renameAction { isRenaming = true } to the container so that activating the button runs that closure. The modifier defines what "rename" means for this scope, and every RenameButton within it inherits that behavior.

  3. Drive the rename through your own state

    Because the action is an ordinary closure, you connect it to your model however you like. Here it flips the @State private var isRenaming flag, which your UI can observe to begin editing the underlying @State private var name — for instance by presenting an editable field or focusing a text control.

  4. Let RenameButton adapt to its context

    RenameButton inherits the presentation of the controls around it, so the same RenameButton() renders as a menu item inside a Menu, and would appear as a toolbar or context-menu action in those containers. You don't restyle it per location — placing it in the right container is what determines how it looks.

Try it — Change the body of .renameAction { isRenaming = true } to also set name = "Renamed" so that tapping RenameButton immediately updates the displayed Text(name), confirming the button is firing your action.

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.

RenameButton.swift
struct RenameButtonDemo: View {
    @State private var name = "Untitled"
    @State private var isRenaming = false

    var body: some View {
        VStack(spacing: 16) {
            Text(name)
                .font(.headline)

            Menu("Options") {
                RenameButton()
                Button("Duplicate") { }
            }
            .renameAction { isRenaming = true }
        }
        .padding()
    }
}
Live preview
Untitled Options
Untitled Options
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →