0
0
iOS Swiftmobile~5 mins

Keyboard management in iOS Swift

Choose your learning style9 modes available
Introduction

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.

When a user taps a text field and the keyboard covers the input area.
When you want to scroll the screen automatically to keep the active text field visible.
When you want to dismiss the keyboard by tapping outside the text field.
When you want to adjust the layout dynamically as the keyboard appears or disappears.
Syntax
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.

Examples
Listen for keyboard showing event.
iOS Swift
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
Get keyboard size to adjust UI.
iOS Swift
@objc func keyboardWillShow(notification: Notification) {
  if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
    print("Keyboard height: \(keyboardFrame.height)")
  }
}
Dismiss keyboard when user taps outside.
iOS Swift
view.endEditing(true)
Sample App

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.

iOS Swift
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)
  }
}
OutputSuccess
Important Notes

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.

Summary

Keyboard management keeps input fields visible when typing.

Listen to keyboard show/hide notifications to adjust UI.

Dismiss keyboard by tapping outside input areas.