EditButton

A button that toggles the edit mode environment value.

ComponentiOS 13.0+EditButton in SwiftUI

Declaration

React
import { EditButton } from 'swiftui-react-native';
Swift
nonisolated struct EditButton

Initializers

One SwiftUI initializer backs <EditButton>.

React
<EditButton />
Swift
nonisolated init()

Takes no props.

Also takes the common view props: modifiers, style and testID.

Overview

An edit button toggles the environment's EnvironmentValues.editMode value for content within a container that supports edit mode. In the following example, an edit button placed inside a NavigationView supports editing of a List:

@State private var fruits = [
    "Apple",
    "Banana",
    "Papaya",
    "Mango"
]

var body: some View {
    NavigationView {
        List {
            ForEach(fruits, id: \.self) { fruit in
                Text(fruit)
            }
            .onDelete { fruits.remove(atOffsets: $0) }
            .onMove { fruits.move(fromOffsets: $0, toOffset: $1) }
        }
        .navigationTitle("Fruits")
        .toolbar {
            EditButton()
        }
    }
}

Because the ForEach in the above example defines behaviors for DynamicViewContent.onDelete(perform:) and DynamicViewContent.onMove(perform:), the editable list displays the delete and move UI when the user taps Edit. Notice that the Edit button displays the title "Done" while edit mode is active:

You can also create custom views that react to changes in the edit mode state, as described in EditMode.

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • View

See also

More in Views.