跳转至
Structure

EditButton

A button that toggles the edit mode for the current edit scope.

Declaration

struct EditButton : View

Overview

An EditButton toggles the EditMode (passed via editMode) for content within a container that supports EditMode.active. For example, an EditButton placed inside the toolbar of a NavigationView enables the editing of a List:

 struct ExampleView: View {
     @State var fruits = ["🍌", "🍏", "🍑"]

     var body: some View {
         NavigationView {
             List {
                 ForEach(fruits, id: \.self) { fruit in
                     Text(fruit)
                 }
                 .onDelete { offsets in
                     fruits.remove(atOffsets: offsets)
                 }
             }
             .toolbar {
                 EditButton()
             }
         }
     }
 }
A gif showing a view with an "Edit" button right aligned above a list containg three fruit emojis as members; when clicked, the word "Edit" is replaced by "Done" and a red circle appears next to each list item, allowing it to be deleted.

Editing a List using EditButton

An EditButton placed in the navigation bar of a NavigationView with a List in it can be used to provide an edit button for the List.

struct ExampleView: View {
    @State var fruits = ["🍌", "🍏", "🍑"]

    var body: some View {
        NavigationView {
            List {
                ForEach(fruits, id: \.self) { fruit in
                    Text(fruit)
                }
                .onDelete { offets in
                    fruits.remove(atOffsets: offets)
                }
            }
            .toolbar {
                EditButton()
            }
        }
    }
}
A gif showing a view with an "Edit" button right aligned above a list containg three fruit emojis as members; when clicked, the word "Edit" is replaced by "Done" and red "Delete" button appears in line with any list item when it is slid to the left.

The title and appearance of an EditButton is determined by the system and cannot be overriden.

Availability

iOS 13.0+

Topics


Type Alias

Body The type of view representing the body of this view.


Initializer

init() Creates an edit button.


Instance Property

body The content and behavior of the view.