NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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))) {

                            .frame(width: 44, height: 44)

                        VStack(alignment: .leading) {
                .onDelete(perform: removeProblems)
            .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.



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) {

Edit: Included both buttons :D


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!


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)


Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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

Reply to this topic…

You need to create an account or log in to reply.

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.