TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: SOLVED: Delete on Swipe

Forums > SwiftUI

I have added a delete on swipe in a list:

.onDelete(perform: deleteBook)

    private func deleteBook(at offsets: IndexSet) {
        offsets.forEach { index in
            let book = books[index]
            let bookData = BookData(
                bookID: book.bookID ?? "",
                isbn: book.isbn ?? "",
                title: book.title ?? "",
                subtitle: book.subtitle ?? "",
                authors: book.authors ?? "",
                description: book.bookDescription ?? "",
                publishedDate: book.publishedDate ?? "",
                publisher: book.publisher ?? "",
                language: book.language ?? "",
                thumbnailURL: book.thumbnailURL ?? "",
                tags: [book.tag1, book.tag2, book.tag3].compactMap { $0 },
                borrowCounter: Int(book.borrowCounter)
            )
            bookToRestore = bookData
            databaseManager.deleteBook(book) { success in
                DispatchQueue.main.async {
                    if success {
                        books.remove(at: index)
                        showToast = true
                    }
                }
            }
        }
    }

This worked without any problems. Afterwards I added a hidekeyboard function in the view, since then the delete on swipe no longer works properly.

        .simultaneousGesture(TapGesture().onEnded {
            hideKeyboard()

                private func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }

The .simultaneousGesture(TapGesture().onEnded of the hidekeyboard is applied to the entire list.

If I swipe all the way to the left, the entry is still deleted. However, if I only swipe a little so that the delete button appears, the button does not work. As soon as I comment out the hidekeyboard, everything works again. Can anyone help me with the reason for this?

2      

And what is the purpose of that hideKeyboard function? Not 100% sure if you just need to dismiss on scroll but why don't you get rid of that completely and use the following modifier for the List:

scrollDismissesKeyboard(ScrollDismissesKeyboardMode)—This modifier determines the behavior of the keyboard when the drag gesture begins. The argument is a structure with the type properties automatic, immediately, interactively, and never (default).”

2      

Thanks, this works totally fine! I am new to Swift, and I am learning with this App - so the hidekeyboard func was my workaround so the keyboard dismisses after using the searchbar by clicking somewhere. I didnt knew the scrollDismissesKeyboard modifier - so thanks a lot!

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.