Skip to content

Instantly share code, notes, and snippets.

@tomlobato
Last active January 23, 2016 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomlobato/a04af45c9e3dc409fecb to your computer and use it in GitHub Desktop.
Save tomlobato/a04af45c9e3dc409fecb to your computer and use it in GitHub Desktop.
UIScrollVIew & keyboard
import UIKit
// To make it work, add this to your child viewController:
// override func viewDidLoad() {
// super.viewDidLoad()
// baseScrollView = scrollView
// myTextView.delegate = self // Set delegate = self for each UITextView
// }
extension UIEdgeInsets {
func clone() -> UIEdgeInsets {
return UIEdgeInsets(top: self.top, left: self.left, bottom: self.bottom, right: self.right)
}
}
class BaseViewController: UIViewController, UITextViewDelegate {
var baseScrollView: UIScrollView!
var savedContentInsets: UIEdgeInsets!
var activeTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
onKeyboardWillShow(keyboardSize.height)
}
}
func keyboardDidShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
onKeyboardDidShow(keyboardSize.height)
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
onKeyboardWillHide(keyboardSize.height)
}
}
func onKeyboardWillShow(height: CGFloat) {
if baseScrollView != nil {
if (savedContentInsets == nil) {
savedContentInsets = baseScrollView.contentInset.clone()
}
var newContentInsets = baseScrollView.contentInset.clone()
newContentInsets.bottom = height
baseScrollView.contentInset = newContentInsets
baseScrollView.scrollIndicatorInsets = newContentInsets
}
}
func onKeyboardDidShow(height: CGFloat) {
// ensure UITextView appears as it is not handled automagically by IOS SDK
if activeTextView != nil {
ensureTextViewAppears()
}
}
func onKeyboardWillHide(height: CGFloat) {
if baseScrollView != nil {
if savedContentInsets != nil {
baseScrollView.contentInset = savedContentInsets
baseScrollView.scrollIndicatorInsets = savedContentInsets
savedContentInsets = nil
}
}
}
func ensureTextViewAppears(){
let originalContentOffsetY = -savedContentInsets.top
let fieldNewTopMargin = CGFloat(25)
let yChangeForField = activeTextView.frame.origin.y - fieldNewTopMargin
baseScrollView.setContentOffset(
CGPoint(
x: baseScrollView.contentOffset.x,
y: originalContentOffsetY + yChangeForField
),
animated: true
)
}
func textViewDidBeginEditing(textView: UITextView) {
activeTextView = textView
}
func textViewDidEndEditing(textView: UITextView) {
activeTextView = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment