Skip to content

Instantly share code, notes, and snippets.

@thomaspaulmann
Created June 12, 2016 10:26
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 thomaspaulmann/54f8c5a20205f108a955da2e903a67d1 to your computer and use it in GitHub Desktop.
Save thomaspaulmann/54f8c5a20205f108a955da2e903a67d1 to your computer and use it in GitHub Desktop.
It's such a pain to handle the keyboard experience in iOS. Especially when you have bottoms on the button that you will show and so on. Here is a simple Extension that handle's the stuff for you. Start/Stop your observation and provide a bottom constraint for the view that you wanna handle. That's it!
//
// ExampleViewController.swift
//
// Created by Thomas Paul Mann on 11/06/16.
// Copyright © 2016 Thomas Paul Mann. All rights reserved.
//
import UIKit
class ExampleViewController: UIViewController {
// MARK: - Outlets
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
startKeyboardObservation()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
stopKeyboardObservation()
}
// MARK: - Keyboard Observation
override func bottomConstraint() -> NSLayoutConstraint? {
return bottomConstraint
}
}
//
// UIViewController+Keyboard.swift
//
// Created by Thomas Paul Mann on 12/06/16.
// Copyright © 2016 Thomas Paul Mann. All rights reserved.
//
import UIKit
extension UIViewController {
func startKeyboardObservation() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LanguageTranslationViewController.animateWithKeyboard(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LanguageTranslationViewController.animateWithKeyboard(_:)), name: UIKeyboardWillHideNotification, object: nil)
}
func stopKeyboardObservation() {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
// Inspired by this discussion: http://stackoverflow.com/questions/25693130/move-textfield-when-keyboard-appears-swift
func animateWithKeyboard(notification: NSNotification) {
guard let
constraint = bottomConstraint(),
userInfo = notification.userInfo,
keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue().height,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt
else {
return
}
constraint.constant = (notification.name == UIKeyboardWillShowNotification) ? keyboardHeight : 0
let options = UIViewAnimationOptions(rawValue: curve << 16)
UIView.animateWithDuration(duration,
delay: 0,
options: options,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
// Override this method to provide your own Constraint
func bottomConstraint() -> NSLayoutConstraint? {
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment