Reputation:
So I have a ViewController, inside it i have a TableView and at the bottom of it a View that contains a TextField and a Button.
When the keyboard appears I tried to put the View at the bottom just above the keyboard and my View appears only if I put my View in front like this : self.view.bringSubviewToFront(SendMessageView) (Just to be clear my View is visble and I can see it, it only dissapears when I try to move it on top of the keyboard when it appears) So now that I can see my View when I mooved it above the keyboard, when I begin to type something my View dissapears instantly and I hane no clue why and I don't know how to fix this. I hope I explained my problem clearly Below this you'll find my entire code :
import UIKit
import Foundation
class Chat_user: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
@IBOutlet weak var SendMessageView: UIView!
@IBOutlet weak var SendMessageInput: UITextField!
@IBOutlet weak var SendButton: UIButton!
@IBOutlet weak var MessageList: UITableView!
var cellMessage: CustomMessage!
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.test))
singleTapGestureRecognizer.numberOfTapsRequired = 1
singleTapGestureRecognizer.isEnabled = true
singleTapGestureRecognizer.cancelsTouchesInView = false
self.view.addGestureRecognizer(singleTapGestureRecognizer)
SendMessageInput.delegate = self
self.view.bringSubviewToFront(SendMessageView)
self.view.bringSubviewToFront(SendMessageInput)
self.view.bringSubviewToFront(SendButton)
self.view.sendSubviewToBack(MessageList)
self.tabBarController?.tabBar.isHidden = true
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func test(sender: UITapGestureRecognizer) {
self.view.endEditing(true)
}
override func viewWillDisappear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
}
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.SendMessageView.frame.origin.y -= keyboardSize.height
self.MessageList.frame.origin.y -= keyboardSize.height
let indexPath = NSIndexPath(row: self.count - 1, section: 0)
self.MessageList.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.SendMessageView.frame.origin.y += keyboardSize.height
self.MessageList.frame.origin.y += keyboardSize.height
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.count = 10
return (self.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
self.cellMessage = tableView.dequeueReusableCell(withIdentifier: "message", for: indexPath) as? CustomMessage
return (cellMessage)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
}
Thanks in advance,
Arkning
Upvotes: 1
Views: 223
Reputation: 100503
It's a known issue with frame-layout , you need to drag the bottom constraint of the SendMessageView
and inside
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.SendMessageViewBottCon.constant += keyboardSize.height
self.view.layoutIfNeeded()
let indexPath = NSIndexPath(row: self.count - 1, section: 0)
self.MessageList.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
self.SendMessageViewBottCon.constant = 0
self.view.layoutIfNeeded()
}
If you need animation insert
UIView.animate(withDuration: 0.25, animations: {
self.view.layoutIfNeeded()
})
Upvotes: 1