Skip to content

Instantly share code, notes, and snippets.

@cscouto
Created March 16, 2018 19:36
Show Gist options
  • Save cscouto/b796abc79ab2287c71ddf458a7311efc to your computer and use it in GitHub Desktop.
Save cscouto/b796abc79ab2287c71ddf458a7311efc to your computer and use it in GitHub Desktop.
SWIFT - Moving UITextField with the keyboard
import UIKit
class MessageVC: UIViewController {
@IBOutlet weak var textViewMessage: UITextView!
@IBOutlet weak var heightMessage: NSLayoutConstraint!
@IBOutlet var viewMessage: UIView! // the view that will be attached to the keyboard
var messageVM: MessageViewModel!
let MESSAGE_INITIAL_SIZE: CGFloat = 44
let MESSAGING_PADDING: CGFloat = 16
var keyboardHeight:CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
override var inputAccessoryView: UIView? {
return viewMessage
}
override var canBecomeFirstResponder: Bool {
return true
}
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
messageVM.fetchData()
}
@IBAction func sendMessage(_ sender: UIButton) {
guard let message = textViewMessage.text, message.count > 0 else {
return
}
let valueIncreasedOnTextView = textViewMessage.frame.height - MESSAGE_INITIAL_SIZE
textViewMessage.text = ""
self.keyboardHeight -= valueIncreasedOnTextView
checkKeyboard(textView: textViewMessage)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
UIView.animate(withDuration: 0.4, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.keyboardHeight = keyboardSize.height
self.checkKeyboard(textView: self.textViewMessage)
}, completion: {_ in
if self.keyboardHeight > 60 {
if self.messageVM.count > 0 {
let indexPath = IndexPath(row: self.messageVM.count-1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.bottom, animated: true)
}
}
})
}
}
func checkKeyboard(textView: UITextView){
if textView.contentSize.height > MESSAGE_INITIAL_SIZE {
heightMessage.constant = textView.contentSize.height
for constraint in (inputAccessoryView?.constraints)! {
if constraint.constant == viewMessage.frame.height {
constraint.constant = MESSAGING_PADDING + textView.contentSize.height
tableView.contentInset.bottom = keyboardHeight
}
}
}else{
heightMessage.constant = MESSAGE_INITIAL_SIZE
for constraint in (inputAccessoryView?.constraints)! {
if constraint.constant == viewMessage.frame.height {
constraint.constant = MESSAGING_PADDING + MESSAGE_INITIAL_SIZE
tableView.contentInset.bottom = keyboardHeight
}
}
}
}
}
extension MessageVC: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
checkKeyboard(textView: textView)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment