UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

3      

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

5      

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!

3      

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)

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.