NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: SwiftUI "Edit" button works correctly only from time to time

Forums > SwiftUI

Hey. I'm building a simple app that asks the user to input some data and then stores it as elements in the list that are Navigation Links to detail page of each element. I implemented an option to delete elements from the list using .onDelete method. Gesture works great, everything happens correctly. However, "Edit" button is buggy - sometimes it doesn't show "minus" button to delete elements, and sometimes it does. I noticed that when it doesn't work the solution to make it work again is to perform some change in the list; for example I can delete an element. I have no idea what is causing this behavior. Here is the code that probably has a problem somewhere:

    var body: some View {
        NavigationView {

            List {
                ForEach(problems.problems, id: \.self) { problem in
                    NavigationLink(destination: ProblemView(problem: problem, doneProblems: checkYeetedProblems(language: problem.language))) {

                        Image("\(problem.language)")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 44, height: 44)

                        VStack(alignment: .leading) {
                            Text(problem.name)
                                .font(.headline)
                            Text(problem.language)
                        }
                    }
                }
                .onDelete(perform: removeProblems)
            }
            .listStyle(InsetGroupedListStyle())
            .navigationTitle("Tracker")
            .navigationBarItems(leading: EditButton(), trailing:
                                    Button(action: {
                                        self.addingProblem = true
                                    }) {
                                        Image(systemName: "plus")
                                    }
            )
            .sheet(isPresented: $addingProblem) {
                AddProblemView(problems: self.problems)
            }
        }
    }

Note: .sheet is just adding a new element to the list, checkYeetedProblems() is a function that returns an integer from some data. removeProblems() looks as follows:

func removeProblems(at offset: IndexSet) {
        problems.problems.remove(atOffsets: offset)
    }

I will be very grateful for any tips on how to resolve this issue.

1      

Hi,

I had the same problem and the solution was to replace the navigationBarItems with .toolbar. Something like this:

            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {

                                 Button(action: {
                                        self.addingProblem = true
                                    }) {
                                        Image(systemName: "plus")
                                    }

                }

                ToolbarItem(placement: .navigationBarLeading) {
                    EditButton()
                }
            }

Edit: Included both buttons :D

3      

Thanks for the answer @Tiberiu27. I'm surprised that it was the case - I think I would never have thought about it. Everything works perfectly now!

1      

The solution provided here is not only a feasible workaround, but also the official solution provided by Apple - .navigationBarItems() has been deprecated, .toolbar() is recommended instead as the current up-to-date method to add elements to different toolbars (including the NavigationBar)

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.