2

My goal is to have a "narration" message in JSQMessagesViewController. I want this message to be centered on the screen. I am trying to center the message by centering it's message bubble container (contains the bubble and the text). This is my code for it:

class MessagesViewController: JSQMessagesViewController

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
        let message = messages[indexPath.row]

        // Sent message
        if message.senderId() == self.senderId {
            // Normal outgoing message

        }
        // Narration Message
        else if message.senderId() == Constants.lbSenderName {
            cell.textView?.textAlignment = NSTextAlignment.Center

            // new frame -- isn't changing the frame.
            let newX = (cell.frame.width - (cell.messageBubbleContainerView?.frame.width)!)/2.0
            cell.messageBubbleContainerView?.frame = CGRectMake(
                newX,
                (cell.messageBubbleContainerView?.frame.minY)!,
                (cell.messageBubbleContainerView?.frame.width)!,
               (cell.messageBubbleContainerView?.frame.height)!
           )
        }
        // Received message
        else {
            // Normal incoming message
        }

        return cell
    }

}

The cell does not move at all as I would expect it to. I have tried using

cell.messageBubbleContainerView?.translatesAutoresizingMaskIntoConstraints = true

but it makes the bubbles disappear. Any suggestion are appreciated, either to change the layout manually like I'm doing, or something I could use through JSQMessagesViewController.

I want it to look like this: Narration Messages

hoffware
  • 205
  • 6
  • 19
  • I'd suggest you subclass `JSQMessagesCollectionViewCell` and add suitable layouts for your applications. – Cheng-Yu Hsu Oct 25 '15 at 02:20
  • I found this in the [documentation](http://cocoadocs.org/docsets/JSQMessagesViewController/7.2.0/Classes/JSQMessagesCollectionViewCell.html) "Warning: You should not try to manipulate any properties of this view, for example adjusting its frame, nor should you remove this view from the cell or remove any of its subviews. Doing so could result in unexpected behavior." So that makes sense. But I'm not sure if I should be subclassing JSQMessagesCollectionViewCell or just subclassing CollectionViewCell if I only need text essentially? – hoffware Oct 26 '15 at 04:21

1 Answers1

1

This is not currently supported by library. Your code above isn't changing anything because those changes are immediately overwritten by the library.

The only want to currently achieve this is to provide your own cell subclasses. You can register your cells with the collection view, then set the incoming and outgoing cell identifier properties.

Relevant docs: http://cocoadocs.org/docsets/JSQMessagesViewController/7.2.0/Classes/JSQMessagesViewController.html

jessesquires
  • 220
  • 2
  • 5
  • Could you provide a code snippet how to achieve that? Do I need to create the cell in the storyboard some how? – Yaron Levi Dec 26 '15 at 02:48