Home > Enterprise >  SwiftUI EditButton in NavigationView not activating edit mode
SwiftUI EditButton in NavigationView not activating edit mode

Time:09-26

In full view, it is impossible to edit that particular list. The edit button functionality is not appeared in my latest Xcode version (14.0)

import SwiftUI

struct ListTutorialBootcamp: View {
    
    func delete(indexSet: IndexSet) {
        fruits.remove(atOffsets: indexSet)
    }
    
    @State private var fruits: [String] = ["Apple", "banana", "grape", "Orange", "Peach"]
    var body: some View {
        NavigationView {
            List {
               
                    ForEach(fruits, id: \.self)  {
                        fruit in Text(fruit.capitalized)
                    }
                    .onDelete(perform: delete)
                   
                
            }
        }
        .navigationTitle("Grocery List")
        .navigationBarItems(leading: EditButton())
       
    }
}

struct ListTutorialBootcamp_Previews: PreviewProvider {
    static var previews: some View {
        ListTutorialBootcamp()
    }
}

CodePudding user response:

You need to add navigationTitle and navigationBarItems modifiers on your List instead of NavigationView.

var body: some View {
    NavigationView {
        List {
            ForEach(fruits, id: \.self) { fruit in
                Text(fruit.capitalized)
            }
            .onDelete(perform: delete)                
        }
        .navigationTitle("Grocery List")
        .navigationBarItems(leading: EditButton())
    }
}

Note:- If you are targeting iOS16 as the minimum version then use the new NavigationStack instead of the deprecated NavigationView.

  • Related