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()
    }
}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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: stackoverflow.com/a/73701150/18547664
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.
iOS 17 minimum - it doesn't work for some reason: stackoverflow.com/questions/78889869/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.