Skip to content

Instantly share code, notes, and snippets.

@jazzedge
Last active December 16, 2017 18:37
Show Gist options
  • Save jazzedge/93a911f7edcd04e6a9839fa179eeeaf0 to your computer and use it in GitHub Desktop.
Save jazzedge/93a911f7edcd04e6a9839fa179eeeaf0 to your computer and use it in GitHub Desktop.
See: https://stackoverflow.com/questions/31774006/how-to-get-height-of-keyboard-swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textFieldBottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func keyboardWillShow(notification:NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
print(keyboardHeight)
}
if let info = notification.userInfo {
let rect:CGRect = info["UIKeyboardFrameEndUserInfoKey"] as! CGRect
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.25, animations: {
self.view.layoutIfNeeded()
self.textFieldBottomConstraint.constant = rect.height + 20
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment