所以这是控制器层次结构tabBarController->一些控制器和聊天通道控制器。
,此聊天频道控制器也是导航控制器。当我选择行时,它将推送到MessageViewController类的聊天控制器。
我这里有2个问题,其中1个是次要专业。
次要角色是头像。
func avatarSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
print("delegate method called")
return .zero
}
我在其中一个代表中将其设置为零,但它仍然显示。而且它从不打印该语句。
主要的一点是输入栏根本没有显示。 我的pod文件中已经有InputBarAccessoryView 和
messageInputBar.delegate = self as InputBarAccessoryViewDelegate
extension ChatViewController: InputBarAccessoryViewDelegate {
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
print("text")
}
}
答案 0 :(得分:0)
要解决此问题,我必须:
import InputBarAccessoryView
所以您不必这样做:
messageInputBar.delegate = self as InputBarAccessoryViewDelegate
并在您的viewDidLoad()中设置这些委托(颜色可选)
super.viewDidLoad()
guard let userId = Auth.auth().currentUser?.uid else{return}
Database.fetchUserWithUID(uid: userId) { (user) in
self.currentLogginUser = user
}
messageInputBar.delegate = self
maintainPositionOnKeyboardFrameChanged = true
messageInputBar.inputTextView.tintColor = .yellow
messageInputBar.sendButton.setTitleColor(.purple, for: .normal)
messagesCollectionView.messagesDataSource = self
messagesCollectionView.messagesLayoutDelegate = self
messagesCollectionView.messagesDisplayDelegate = self
messagesCollectionView.messageCellDelegate = self
messageInputBar.leftStackView.alignment = .center
messageInputBar.setLeftStackViewWidthConstant(to: 50, animated: false)
getMessages()
}
然后使用实时数据库-Firebase
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
guard let recipient = self.recipient else{return}
guard let uid = Auth.auth().currentUser?.uid else{return}
let ref = Database.database().reference().child("messages")
let childRef = ref.childByAutoId()
let values = ["sender": uid, "text": text, "recipient": recipient.uid, "time": Date().timeIntervalSince1970.description]
childRef.updateChildValues(values) { (err, ref) in
if let err = err{
print(err)
return
}
let userMessageRef = Database.database().reference().child("user-messages").child(uid).child(recipient.uid)
//add chat id generated from AutoId
guard let messageId = childRef.key else{return}
userMessageRef.updateChildValues([messageId: 1])
let recipientMessageRef = Database.database().reference().child("user-messages").child(recipient.uid).child(uid)
recipientMessageRef.updateChildValues([messageId:1])
}
inputBar.inputTextView.text = ""
}
}
答案 1 :(得分:0)
显示输入栏的解决方案:
class ViewController: MessagesViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.becomeFirstResponder()
}
}
来源:https://github.com/MessageKit/MessageKit/issues/501#issuecomment-361913168