I am trying to build a chat app in swift using JSQMessagesViewController. Unfortunately, messages do not appear on screen. The only thing visible is the input bar.
I followed the instructions in this tutorial: https://www.raywenderlich.com/122148/firebase-tutorial-real-time-chat except I didn't strat with the demo app but used my own application as a starting point. Thus, I am not using a storyboard or a navigationController.
This is my chatController:
import Foundation
import UIKit
import Foundation
import JSQMessagesViewController
class chatController: JSQMessagesViewController {
var messages = [JSQMessage]()
var outgoingBubbleImageView: JSQMessagesBubbleImage!
var incomingBubbleImageView: JSQMessagesBubbleImage!
private func setupBubbles() {
let factory = JSQMessagesBubbleImageFactory()
outgoingBubbleImageView = factory.outgoingMessagesBubbleImageWithColor(UIColor.jsq_messageBubbleBlueColor())
incomingBubbleImageView = factory.incomingMessagesBubbleImageWithColor(UIColor.jsq_messageBubbleLightGrayColor())
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Sprout"
setupBubbles()
collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSizeZero
collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero
print(self.view.frame)
}
override func viewDidLayoutSubviews() {
self.collectionView.frame = self.view.frame
//self.collectionView.setNeedsLayout()
//self.collectionView.layoutIfNeeded()
print("collectionview frame: \(self.collectionView.frame)")
print("view frame: \(self.view.frame)")
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// messages from someone else
addMessage("foo", text: "Hey person!")
// messages sent from local sender
addMessage(senderId, text: "Yo!")
addMessage(senderId, text: "I like turtles!")
// animates the receiving of a new message on the view
finishReceivingMessage()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let message = messages[indexPath.item] // 1
if message.senderId == senderId { // 2
return outgoingBubbleImageView
} else { // 3
return incomingBubbleImageView
}
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
let message = messages[indexPath.item]
if message.senderId == senderId {
cell.textView!.textColor = UIColor.whiteColor()
} else {
cell.textView!.textColor = UIColor.blackColor()
}
return cell
}
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
self.addMessage(senderId, text: text);
finishSendingMessage()
}
func addMessage(id: String, text: String) {
print("add message: \(text)")
let message = JSQMessage(senderId: id, displayName: "", text: text)
messages.append(message)
}
}
which I am showing calling this code from my rootViewController:
let userID = "abc123"
let userName = "randomUserName"
let chatVc = chatController()
chatVc.senderId = userID
chatVc.senderDisplayName = userName
self.presentViewController(chatVc, animated: true, completion: nil)
I tried to clean the project without success. Can somebody help me display the message bubbles? Thanks for any help! Kind regards, Alex