Yes exacly my View is below the bottom of the tableview.
I tried it but I still have the same issue i'm posting my code so you can see what am I doing wrong.
import UIKit
import Foundation
import SocketIO
import SwiftyJSON
import Alamofire
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
var hasMoved = false
var messages: Hoze_Messages!
var originalFrame: CGRect = .zero
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)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
originalFrame = self.SendMessageView.frame
}
@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 {
if !self.hasMoved {
self.SendMessageView.frame.origin.y -= keyboardSize.height
self.MessageList.frame.origin.y -= keyboardSize.height
if (self.count > 0) {
let indexPath = NSIndexPath(row: self.count - 1, section: 0)
self.MessageList.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
}
self.hasMoved = true
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if hasMoved {
self.SendMessageView.frame = originalFrame
//self.SendMessageView.frame.origin.y += keyboardSize.height
self.MessageList.frame.origin.y += keyboardSize.height
}
hasMoved = false
}
}
func json(from object:Any) -> String? {
guard let data = try? JSONSerialization.data(withJSONObject: object, options: []) else {
return nil
}
return String(data: data, encoding: String.Encoding.utf8)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let defaults = UserDefaults.standard
let check = defaults.integer(forKey: "id")
let jsonObject: [String: Any] = [
"user_id1": check,
"user_id2": My_Friends.result![y].id,
]
socket?.on("getmessages") { data, ack in
print("DATA ========================", data)
print("DATA2 ========================", data[0])
let mstring = data[0] as! String
var dictonary:NSDictionary?
if let test = mstring.data(using: String.Encoding.utf8) {
do {
dictonary = try JSONSerialization.jsonObject(with: test, options: []) as? NSDictionary
} catch let error as NSError {
print(error)
}
}
let azer = Hoze_Messages(json: dictonary as! Dictionary<String,Any>) //as! Dictionary<String,Any>)
print(azer)
}
socket?.emit("getmessages", jsonObject)
return (self.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
self.cellMessage = tableView.dequeueReusableCell(withIdentifier: "message", for: indexPath) as? CustomMessage
if (self.count > 0) {
self.cellMessage.MessageReceivedText.text = self.messages.result?[indexPath.row].text
}
return (cellMessage)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
@IBAction func sendMessage(sender: UIButton) {
let defaults = UserDefaults.standard
let check = defaults.integer(forKey: "id")
let name = defaults.string(forKey: "username")
socket?.on("messages") { dataArray, ack in
print("Messages ========================", dataArray)
print("Messages ========================", ack)
}
let jsonObject: [String: Any] = [
"sender_id": String(check),
"recipient_id": String(My_Friends.result![y].id!),
"sender": name!,
"recipient": My_Friends.result![y].user_name!,
"text": self.SendMessageInput.text!,
"status": "0",
"type": "1"
]
print("JSON =====================" , jsonObject)
socket?.emit("message", jsonObject)
self.SendMessageInput.text = ""
//self.MessageList.reloadData()
}
}