Overview
The Resign subspec enables “tap-to-dismiss” functionality, allowing users to dismiss the keyboard by tapping anywhere outside of the active text field. This provides an intuitive way to close the keyboard without requiring a Done button.Installation
CocoaPods
Podfile
Swift Package Manager
The Resign functionality is included in the main package:Basic Setup
1
Import the library
AppDelegate.swift
2
Enable resign on touch outside
AppDelegate.swift
3
Test tap-to-dismiss
Run your app, tap on a text field to show the keyboard, then tap anywhere outside the text field to dismiss it.
How It Works
When enabled, IQKeyboardManager:- Adds a tap gesture recognizer to the window
- Detects taps outside active text fields
- Dismisses the keyboard when a tap is detected
- Ignores taps on interactive controls (buttons, switches, etc.)
- Handles conflicts with existing gestures
Configuration
Enable/Disable
Programmatic Dismissal
Manually dismiss the keyboard:ViewController.swift
Class-Level Control
Disable for Specific View Controllers
Prevent tap-to-dismiss for specific view controllers:AppDelegate.swift
By default,
UIAlertController and UIInputViewController are in the disabled list.Force Enable for Specific Classes
AppDelegate.swift
Ignore Specific View Types
Prevent tap-to-dismiss when tapping on specific view types:AppDelegate.swift
Gesture Recognizer Access
Access the tap gesture recognizer to handle conflicts:ViewController.swift
Complete Example
ViewController.swift
Advanced Scenarios
Conditional Resign
Enable resign on touch only for specific scenarios:ViewController.swift
Per-View Controller Control
ViewController.swift
Custom Gesture Handling
ViewController.swift
UIScrollView Interaction
Handling tap-to-dismiss with scroll views:ViewController.swift
Resign vs Done Button
Tap-to-Dismiss
Best for:
- Single text field screens
- Simple forms
- Modal text input
- Quick edits
- Natural gesture
- No UI required
- Works anywhere
Done/Toolbar Button
Best for:
- Multi-field forms
- Guided input flows
- Complex forms
- Required validation
- Clear action
- More discoverable
- Can trigger validation
Best Practices
Enable for most screens
Enable for most screens
Tap-to-dismiss is intuitive and should be enabled for most screens. Only disable it when you have specific interaction requirements.
Consider UIControl conflicts
Consider UIControl conflicts
The default configuration ignores taps on buttons and controls, which is usually what you want. Users can tap buttons without dismissing the keyboard.
Test gesture interactions
Test gesture interactions
If your app uses custom gestures, test thoroughly to ensure they don’t conflict with the resign gesture.
Provide visual feedback
Provide visual feedback
Consider adding visual feedback when the keyboard dismisses to help users understand the interaction.
Troubleshooting
Tap-to-dismiss not working
Tap-to-dismiss not working
- Verify
resignOnTouchOutsideis set totrue - Check if the view controller is in
disabledTouchResignedClasses - Ensure no other gesture is blocking the tap gesture
- Check if a custom gesture has higher priority
Gesture conflicts
Gesture conflicts
- Use
require(toFail:)to manage gesture priority - Implement
UIGestureRecognizerDelegatefor custom control - Access
IQKeyboardManager.shared.resignGestureto handle conflicts
Table view cells not responding
Table view cells not responding
- Add
UITableViewCelltotouchResignedGestureIgnoreClasses - Or implement
tableView(_:didSelectRowAt:)to dismiss manually
Related Properties
Bool
default:"false"
Enable tap-to-dismiss keyboard functionality
UITapGestureRecognizer
default:"UITapGestureRecognizer()"
The tap gesture recognizer used for dismiss functionality. Read-only, exposed for handling gesture dependencies.
[UIViewController.Type]
View controller classes where tap-to-dismiss is disabled
[UIViewController.Type]
default:"[]"
View controller classes where tap-to-dismiss is force-enabled
[UIView.Type]
default:"[UIControl.self, UINavigationBar.self]"
View classes that won’t trigger tap-to-dismiss when tapped
Next Steps
Advanced Configuration
Fine-tune keyboard behavior
Toolbar Management
Add navigation buttons to keyboard
Debugging
Troubleshoot resign issues
API Reference
Full API documentation