GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

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.