How to dismiss iOS keyboard by tapping anywhere on the screen (Swift)
Hello everyone! I'm a beginner in Swift and iOS development. I'm writing a simple app — a data entry form. I have several UITextField, and when the user enters text, the keyboard appears normally. But I can't figure out how to dismiss it when the user taps anywhere on the screen (for example, on empty space or the background).\n\nCurrently, I did this: I added a "Done" button to the keyboard's accessory panel, and it works — the keyboard hides. But it's inconvenient. Users are used to just tapping the background and the keyboard disappearing.\n\nI tried adding a gesture recognizer (UITapGestureRecognizer) to the main view, but nothing works for me. The keyboard doesn't respond. Here's my code:\n\n```swift\noverride func viewDidLoad() {\n super.viewDidLoad()\n let tap = UIT
No answers yet.
Log in to leave an answer
Blogs
How to become a C# developer: tips and resume in 2025