Skip to content

Instantly share code, notes, and snippets.

@RodolfoAntonici
Last active December 12, 2018 17:07
Show Gist options
  • Save RodolfoAntonici/34f942798512d2c007a26483f81cbcfd to your computer and use it in GitHub Desktop.
Save RodolfoAntonici/34f942798512d2c007a26483f81cbcfd to your computer and use it in GitHub Desktop.
Implement keyboard avoidance on any UIScrollView
// Created by Rodolfo Antonici on 12/12/18.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
extension UIScrollView {
/// Begin listening to keyboard notifications to add or remove the keyboard size as bottom inset
func beginKeyboardAvoidance() {
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShowWithNotification(notification:)),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHideWithNotification(notification:)),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
/// End listening to keyboard notifications
func endKeyboardAvoidance() {
NotificationCenter.default.removeObserver(self)
}
/// Add bottom inset on the scroll view the size
func addDefaultKeyboardPadding(animated: Bool = true) {
let keyboardDefaultHeight: CGFloat = 216
if animated {
UIView.animate(withDuration: 0.25) {
self.contentInset = UIEdgeInsets(top: self.contentInset.top,
left: self.contentInset.left,
bottom: keyboardDefaultHeight,
right: self.contentInset.right)
}
}
else {
contentInset = UIEdgeInsets(top: contentInset.top,
left: contentInset.left,
bottom: keyboardDefaultHeight,
right: self.contentInset.right)
}
}
/// Discussion: This is can be called when the screen already starts with the keyboard shown
/// the fixed value is based on the value returned on the notifications.
/// If you know a better way, please do it and comment it below.
@objc fileprivate func keyboardWillShowWithNotification(notification: NSNotification) {
let info = notification.userInfo!
guard let keyboardFrame: CGRect = info[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
let bottomInset = keyboardFrame.height != 0 ? keyboardFrame.height : 0
UIView.animate(withDuration: 0.25) {
self.contentInset = UIEdgeInsets(top: self.contentInset.top,
left: self.contentInset.left,
bottom: bottomInset,
right: self.contentInset.right)
}
}
@objc fileprivate func keyboardWillHideWithNotification(notification: NSNotification) {
UIView.animate(withDuration: 0.25) {
self.contentInset = UIEdgeInsets(top: self.contentInset.top,
left: self.contentInset.left,
bottom: 0,
right: self.contentInset.right)
}
}
}
@RodolfoAntonici
Copy link
Author

RodolfoAntonici commented Dec 27, 2016

HEY, it doesn't work magically, you should call it on an UIScrollView, UITableView, UICollectionView beginKeyboardAvoidance() when the element will be show or on viewDidLoad() and should call endKeyBoardAvoidance() when the View is removed or on the deinit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment