Responding to Keyboard Presentation/Dismissal in Swift

Thumbnail.jpg

Nearly all iOS apps need to accept input from the user at some point. The problem is the keyboard presents from the bottom of the screen, on top of everything, which can cover up some of your content (or worse, the text field where the user is typing!) In these situations it’s important to respond to the keyboard entering/exiting the screen so you can move your content up into view.

The Keyboard Frame Change Notification

The UIResponder class has a some very useful notifications built in. The one we’re going to use is called keyboardWillChangeFrameNotification. That notification fires when the keyboard is about to present, about to dismiss, or when something is about to change on it such as the prefill text bar showing/hiding.

Subscribing to the Notification

Subscribing to keyboardWillChangeFrameNotification is as easy as:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

As you can see, I’m telling my app to execute the keyboardWillChangeFrame function whenever the notification fires. That’s where all the work of moving views up/down is going to happen.

The keyboardWillChangeFrame Function

Simply subscribing to the notification isn’t enough - we’ll need to pull out the keyboard height information from the notification so we know where the top of the keyboard is going to be. Here’s a look at a very simple implementation of keyboardWillChangeFrame:

@objc func keyboardWillChangeFrame(notification: NSNotification) {
    if let userInfo = notification.userInfo { // 1
        let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue // 2
        let endFrameY = endFrame?.origin.y ?? 0 // 3
        let duration: TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0 // 4
}

Let’s walk through this code line by line to see what exactly is going on.

  1. First, we’re making sure the notification has a userInfo dictionary. This is the dictionary that contains all of the information we need to determine how to move our views around with the keyboard

  2. Next, we’re getting the frame for where the keyboard will be after it presents/dismisses.

  3. For convenience I like to pull the Y position for the top of the keyboard out and use that. Depending on what you’re trying to do, you may want to skip this part

  4. Finally, we’re getting the duration of the keyboard animation. While it’s not necessary, I like to use the same duration it takes to move the keyboard for my animations moving views out of the way. It just helps to make everything look a lot smoother.

That’s pretty much it! Now you know exactly where the keyboard is going to be, and you can take that information and use it to move your views accordingly.

Previous
Previous

How to Layer Views in SwiftUI Using ZStack

Next
Next

Working with Dates in Swift