Keyboard management helps your app move content so users can see what they type. It stops the keyboard from hiding important parts of the screen.
Keyboard management in iOS Swift
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
@objc func keyboardWillShow(notification: Notification) {
// Adjust UI here
}Use NotificationCenter to listen for keyboard events.
Implement methods to adjust your UI when keyboard shows or hides.
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)@objc func keyboardWillShow(notification: Notification) {
if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
print("Keyboard height: \(keyboardFrame.height)")
}
}view.endEditing(true)
This example moves the screen up when the keyboard appears so the text field stays visible. It moves back down when the keyboard hides. Tapping outside the text field closes the keyboard.
import UIKit class ViewController: UIViewController, UITextFieldDelegate { let textField = UITextField() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white textField.borderStyle = .roundedRect textField.placeholder = "Type here" textField.delegate = self textField.translatesAutoresizingMaskIntoConstraints = false view.addSubview(textField) NSLayoutConstraint.activate([ textField.centerXAnchor.constraint(equalTo: view.centerXAnchor), textField.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50), textField.widthAnchor.constraint(equalToConstant: 200) ]) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) view.addGestureRecognizer(tapGesture) } @objc func keyboardWillShow(notification: Notification) { if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect { let keyboardHeight = keyboardFrame.height UIView.animate(withDuration: 0.3) { self.view.frame.origin.y = -keyboardHeight / 2 } } } @objc func keyboardWillHide(notification: Notification) { UIView.animate(withDuration: 0.3) { self.view.frame.origin.y = 0 } } @objc func dismissKeyboard() { view.endEditing(true) } deinit { NotificationCenter.default.removeObserver(self) } }
Always remove observers if you add them manually to avoid memory leaks.
Use view.endEditing(true) to hide the keyboard easily.
Adjusting the view's frame is a simple way to move content, but using scroll views is better for complex layouts.
Keyboard management keeps input fields visible when typing.
Listen to keyboard show/hide notifications to adjust UI.
Dismiss keyboard by tapping outside input areas.
Practice
Solution
Step 1: Understand keyboard impact on UI
The keyboard can cover input fields, making them hard to see or use.Step 2: Purpose of keyboard management
Managing the keyboard ensures input fields stay visible and accessible when typing.Final Answer:
To keep input fields visible when the keyboard appears -> Option BQuick Check:
Keyboard management = keep inputs visible [OK]
- Thinking keyboard management changes keyboard color
- Believing it disables typing
- Assuming it auto-saves input
Solution
Step 1: Identify method to dismiss keyboard
Calling resignFirstResponder() on a UITextField tells it to stop being active, hiding the keyboard.Step 2: Check other options
becomeFirstResponder() shows keyboard; showKeyboard() and hideKeyboard() do not exist in UIKit.Final Answer:
textField.resignFirstResponder() -> Option AQuick Check:
Dismiss keyboard = resignFirstResponder() [OK]
- Using becomeFirstResponder() which shows keyboard
- Trying non-existent methods like hideKeyboard()
- Not calling any method to dismiss keyboard
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
scrollView.contentInset.bottom = keyboardSize.height
}
}Solution
Step 1: Understand notification observer
The code listens for keyboard showing and calls keyboardWillShow function.Step 2: Adjust scroll view inset
Inside keyboardWillShow, it sets scrollView.contentInset.bottom to keyboard height, pushing content up.Final Answer:
The scroll view's bottom inset increases to avoid keyboard overlap -> Option DQuick Check:
Keyboard show adjusts scroll inset [OK]
- Thinking keyboard hides automatically
- Assuming scroll view scrolls to top
- Ignoring contentInset adjustment
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}Solution
Step 1: Check method override
Overriding touchesBegan requires calling super.touchesBegan to allow normal touch handling.Step 2: Identify missing call
The code misses super.touchesBegan, which can cause touch events to stop propagating.Final Answer:
It should call super.touchesBegan to propagate touch events -> Option CQuick Check:
Override touchesBegan must call super [OK]
- Not calling super.touchesBegan
- Thinking endEditing(true) doesn't dismiss keyboard
- Using wrong method signature
Solution
Step 1: Understand keyboard notifications
keyboardWillShow and keyboardWillHide notify before keyboard appears/disappears, allowing smooth UI changes.Step 2: Adjust view frame on both notifications
Adjusting view frame up on show and back down on hide keeps UI consistent and responsive.Step 3: Evaluate other options
Listening only to keyboardDidShow is late; timer wastes resources; changing keyboard type doesn't solve overlap.Final Answer:
Listen to keyboardWillShow and keyboardWillHide notifications, adjust view frame accordingly -> Option AQuick Check:
Use willShow and willHide notifications for UI moves [OK]
- Using only keyboardDidShow notification
- Polling keyboard height with timer
- Trying to change keyboard type to fix layout
Ready to prove it?
Pass the test and this pattern moves unknown
