Swift - WhatsApp like keyboard dismiss

Viewed 447

I'm developing a message app but I want to dismiss the keyboard when pushing it with my finger.

I know that can be done with

keyboardDismissMode = .interactive

But I don't know how to change tableview's constraints while moving the keyboard (like WhatsApp app for iOS).

Can anyone help me with it?

EDIT:

This is what I already have:

I have a simple UITableView to show my messages

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return messages.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    var celda = UITableViewCell()
    
    let message = messages[indexPath.row]
    
    if message.owner == 0{
        
        celda =  tableView.dequeueReusableCell(withIdentifier: "messageOther")!
    }else{
        
        celda =  tableView.dequeueReusableCell(withIdentifier: "messageMine")!
    }
    
    let fondoView = celda.viewWithTag(1)
    fondoView?.layer.cornerRadius = 10
               
    let messageLabel = celda.viewWithTag(2) as! UILabel
    messageLabel.text = message.text
    
    let fecha = Date(timeIntervalSince1970: TimeInterval(message.date))
    let dateFormatter = DateFormatter()
    dateFormatter.timeStyle = .short
    dateFormatter.timeZone = .current
    
    let dateLabel = celda.viewWithTag(3) as! UILabel
    dateLabel.text = dateFormatter.string(from: fecha)
    
    return celda
}

And in my viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    messagesTableView.keyboardDismissMode = .interactive
    
    getMessages()
}

And the keyboard hides pushing it with the finger, that's all I have so far, but I don't know how to make my UITextField follows the movement of the keyboard, like WhatsApp and iMessage do, also start pushing it from the UITextField instead of the keyboard.

I appreciate all the help!

1 Answers

You can set the keyboard Dismiss Mode to onDrag in ViewDidLoad as below.

tableView.keyboardDismissMode = .onDrag

Check if this is the kind of behavior you want.

Related