0

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()
    }
}
Nijat Namazzade
  • 652
  • 5
  • 15

1 Answers1

2

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.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • 1
    Great answer! `NavigationView` modifiers can't be on NavigationView itself but on any child you want. Also, if you're looking to target both iOS 15 and 16 with `NavigationStack` here's a possibility: https://stackoverflow.com/a/73701150/18547664 – Alexnnd Sep 26 '22 at 08:35
  • Thanks ! It solved my problem, I kept the navigation view as it should be, but the problem was about putting the title and bar items in the correct place. – Nijat Namazzade Sep 26 '22 at 12:36