BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.