Skip to content

Commit 78ef767

Browse files
authored
Merge pull request #8 from csync/df/chatscroll
scroll to bottom when new message appears
2 parents 82cac1f + 6a60c81 commit 78ef767

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

iOS/CSyncChat/ChatViewController.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class ChatViewController: UIViewController {
9696
//Refreshes the view with new data
9797
private func updateView(){
9898
self.tableView.reloadData()
99+
if roomViewModel.numberOfMessages > 0 && numberOfSections > 0 {
100+
tableView.scrollToRow(at: IndexPath(row: roomViewModel.numberOfMessages - 1, section: numberOfSections - 1), at: .bottom, animated: true)
101+
}
99102
}
100103

101104
// MARK: - Segues
@@ -132,7 +135,11 @@ class ChatViewController: UIViewController {
132135
let animationDuration = keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber,
133136
let animationCurve = keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber else {return}
134137

135-
UIView.animate(withDuration: TimeInterval(animationDuration), delay: 0, options: UIViewAnimationOptions(rawValue: animationCurve.uintValue << 16), animations: {self.messageEntryBottomConstraint.constant = 0} , completion: nil)
138+
UIView.animate(withDuration: TimeInterval(animationDuration), delay: 0, options: UIViewAnimationOptions(rawValue: animationCurve.uintValue << 16), animations: {self.messageEntryBottomConstraint.constant = 0}) {_ in
139+
if self.roomViewModel.numberOfMessages > 0 && self.numberOfSections > 0 {
140+
self.tableView.scrollToRow(at: IndexPath(row: self.roomViewModel.numberOfMessages - 1, section: self.numberOfSections - 1), at: .bottom, animated: true)
141+
}
142+
}
136143
}
137144
}
138145
extension ChatViewController : UITextFieldDelegate{
@@ -148,7 +155,6 @@ extension ChatViewController : UITextFieldDelegate{
148155
let message = Message(message: messageText) {
149156
//send message
150157
roomViewModel.send(message)
151-
self.tableView.scrollToNearestSelectedRow(at: .bottom, animated: false)
152158
textField.text = ""
153159
}
154160
}
@@ -190,6 +196,7 @@ extension ChatViewController : UITableViewDataSource, UITableViewDelegate {
190196
cell.msgHeader.attributedText = msgHeader
191197
cell.msgText.text = message.message
192198
cell.msgText.textContainer.lineBreakMode = .byWordWrapping
199+
cell.selectionStyle = .none
193200
return cell
194201
}
195202

0 commit comments

Comments
 (0)