Last active
December 16, 2017 18:37
-
-
Save jazzedge/93a911f7edcd04e6a9839fa179eeeaf0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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