Reputation: 11
I am trying to make the UITextField move so it sits on top of the keyboard (the same as iMessage), but can not figure out what my issue is. I have reviewed questions similar to this one, but have been unsuccessful thus far. The text field appears to move slightly, but ends up behind the keyboard, so hopefully it is a silly coordinate placement issue that someone can point out to me. Another thing to note is that the GIF below is on an iPhone X, however, when I run the app on an iPhone 6 the UITextField moves to the correct location above the keyboard, but moves back underneath it as soon as I type anything. Any help is appreciated!
class ChatViewController: UIViewController {
@IBOutlet weak var messageView: UIView!
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var textMessageField: UITextField!
@IBOutlet weak var contactStatusLabel: UILabel!
@IBOutlet weak var sendButton: UIButton!
@IBOutlet weak var videoChatButton: UIButton!
let cellID = "cellID"
var contactIsOnline = false
var currentUser:G8User = G8User()
var currentContact:G8User = G8User()
var currentConversation : Conversation?
var videoToken = ""
var allMessages : [ChatMessage] = []
var onlineRef:DatabaseReference!
var incomingMessagesRef:DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
sendButton.backgroundColor = UIColor(red: CGFloat(0/255), green: CGFloat(137.0/255.0), blue: CGFloat(249.0/255.0), alpha: 1)
sendButton.layer.cornerRadius = 5
videoChatButton.backgroundColor = UIColor.red
videoChatButton.layer.cornerRadius = 5
collectionView.alwaysBounceVertical = true
collectionView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
collectionView.scrollIndicatorInsets = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
collectionView.register(ChatMessageCell.self, forCellWithReuseIdentifier: cellID)
collectionView.dataSource = self
collectionView.delegate = self
// Used to dismiss keyboard
let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
// Set up keyboard observers
NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
print("SHOWING KEYBOARD")
// TODO: Adjust scrolling for message view
if let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue{
let topOfMessageBox = keyboardFrame.origin.y - self.messageView.frame.height
self.collectionView.frame = CGRect(x: collectionView.frame.origin.x,y: collectionView.frame.origin.y,width: collectionView.frame.width,height: topOfMessageBox - collectionView.frame.origin.y)
self.messageView.frame = CGRect(x: messageView.frame.origin.x,y: topOfMessageBox,width: messageView.frame.width, height: messageView.frame.height)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
print("HIDING KEYBOARD")
self.collectionView.frame = CGRect(x: collectionView.frame.origin.x,y: collectionView.frame.origin.y,width: collectionView.frame.width,height: messageView.frame.origin.y - collectionView.frame.origin.y )
self.messageView.frame = CGRect(x: messageView.frame.origin.x,y: self.view.frame.maxY - messageView.frame.size.height,width: messageView.frame.width,height: messageView.frame.height)
}
Upvotes: 1
Views: 46
Reputation: 1391
Try this UIView
extension something like myTextField.bindToKeyboard()
extension UIView {
func bindToKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification) {
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
var deltaY = endFrame.origin.y - beginningFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
}
Upvotes: 1