Adjust Scroll View Content Inset Based on Keyboard Frame

Posted by

In cases where we have form containing UITextFields embedded in UIScrollView, when keyboard is shown and hidden, we want UIScrollView to be adjusted accordlingly.

We start in the viewWillAppear method by adding the following code:

// https://hashaam.com/2017/09/02/adjust-scroll-view-content-inset-based-on-keyboard-frame
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboardHandler(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboardHandler(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

Then we define the method to handle keyboard notifications:

// https://hashaam.com/2017/09/02/adjust-scroll-view-content-inset-based-on-keyboard-frame
func adjustForKeyboardHandler(notification: Notification) {
// this method assumes scrollView is defined
guard let userInfo = notification.userInfo else { return }
guard let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardFrame = value.cgRectValue
var contentInset: UIEdgeInsets!
switch notification.name {
case Notification.Name.UIKeyboardWillHide:
contentInset = UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 0.0)
case Notification.Name.UIKeyboardWillChangeFrame:
contentInset = UIEdgeInsets(top: 64.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
default:
break
}
scrollView.contentInset = contentInset
scrollView.scrollIndicatorInsets = contentInset
}

This single method handles both cases, with keyboard shown or hidden states.

Notice the top: 64.0 is because of status bar height 20.0 and navigation bar height 44.0. If you do not have UIViewController in embedded in UINavigationController or the navigation bar is hidden, you can set the top value to 20.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s