I have a text view in my project and I want when user tap the text field it moves up equals to the height of the keyboard I used this code for getting height of the keyboard and It gives me the size of the keyboard
static var sizeForOffsetKeyboard = CGFloat()
var heightKeyboard : CGFloat?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}
func keyboardShown(notification: NSNotification) {
if let infoKey = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
let rawFrame = (infoKey as AnyObject).cgRectValue {
let keyboardFrame = view.convert(rawFrame, from: nil)
self.heightKeyboard = keyboardFrame.size.height
levelChatViewController.sizeForOffsetKeyboard = heightKeyboard!
print(levelChatViewController.sizeForOffsetKeyboard)
// Now is stored in your heightKeyboard variable
}
}
But I didn't get the result and the textview won't move up with the height of the keyboard
so here is the codes for offset textview
func textViewDidBeginEditing(_ textView: UITextView) {
animateViewMoving(up: true, moveValue: levelChatViewController.sizeForOffsetKeyboard)
}