Move View with Keyboard Swift: Optimization and Performance

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Move View with Keyboard Using Swift: Optimization and Performance

\n\n

Every iOS developer has encountered a situation where the keyboard rudely covers a UITextField or UITextView. The user taps the field, the keyboard appears, but the interface remains unresponsive. The result is frustration, input errors, and app deletion. Solving this problem in Swift is not just about "move view with keyboard," but a whole science of performance and user experience.

\n\n

In this article, we will break down how to properly implement move view with keyboard using Swift, optimize animations, and avoid common mistakes. You will get ready-to-use code that can be immediately inserted into your project and understand why some approaches slow down the UI while others work perfectly.

\n\n

Why Do Standard Solutions Often Work Poorly?

\n\n

On Stack Overflow, the question "Move view with keyboard using Swift" has garnered thousands of views. This is no coincidence. Many beginners copy the first solution they find from 2015, which uses UIKeyboardWillShowNotification and rigidly changes the UIView frame. The result: jerky animation, memory leaks, and bugs on different iPhone models.

\n\n

Main issues with old approaches:

\n
    \n
  • Directly modifying frame without considering safe area;
  • \n
  • Ignoring the duration and animation curves of the keyboard;
  • \n
  • Lack of handling for screen rotation and multitasking;
  • \n
  • Forgetfulness: notifications are not removed, objects are not cleaned up.
  • \n
\n\n

Modern Swift (5.0+) and UIKit allow you to do everything elegantly and quickly. Let's break down the optimal pattern.

\n\n

Preparation: What We Need

\n\n

To implement move view with keyboard using Swift, we will use:

\n
    \n
  • NotificationCenter to track keyboard events;
  • \n
  • UIView.animate with parameters from the notification;
  • \n
  • UIScrollView as a base container (but it can be done without it);
  • \n
  • Proper memory management (weak self, removeObserver).
  • \n
\n\n

We will write all the code in UIViewController. If you use SwiftUI, the principles are the same, but the API is different (more on this at the end of the article).

\n\n

Step 1: Register Keyboard Notifications

\n\n

The first thing to do is subscribe to two key notifications: UIResponder.keyboardWillShowNotification and UIResponder.keyboardWillHideNotification. These give us the size, duration, and animation curve.

\n\n
override func viewDidLoad() {\n    super.viewDidLoad()\n    \n    NotificationCenter.default.addObserver(\n        self,\n        selector: #selector(keyboardWillShow),\n        name: UIResponder.keyboardWillShowNotification,\n        object: nil\n    )\n    \n    NotificationCenter.default.addObserver(\n        self,\n        selector: #selector(keyboardWillHide),\n        name: UIResponder.keyboardWillHideNotification,\n        object: nil\n    )\n}\n
\n\n

Note: object: nil means we are listening for notifications from any keyboard. This is correct for most cases.

\n\n

Step 2: keyboardWillShow Handler

\n\n

In this method, we extract the keyboard size and animation duration from the notification. Then we move the view up by the keyboard height.

\n\n
@objc private func keyboardWillShow(_ notification: Notification) {\n    guard let userInfo = notification.userInfo,\n          let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,\n          let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,\n          let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else {\n        return\n    }\n    \n    let keyboardHeight = keyboardFrame.height\n    let animation                

Blogs

Book Recommendations