是否可以隐藏消息气泡

is it possible to hide message bubble

我正在使用 JSQMessagesViewController 在我的 iOS 应用程序中实现聊天。我需要在屏幕中间显示一些系统消息(见附图)。我希望我可以通过使用消息底部标签而不显示消息气泡来实现这一点。但是我还没有找到隐藏消息气泡的方法。是否可以?谢谢

我通过覆盖 collectionView sizeForItemAtIndexPath 函数和 return kJSQMessagesCollectionViewCellLabelHeightDefault 的高度,然后 return nil messageBubbleImageDataForItemAtIndexPathavatarImageDataForItemAtIndexPath

override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let userMessageCellSize = super.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAtIndexPath: indexPath)
    let message = messages[indexPath.item]
    if message.type == MessageType.System {
        // for system message, only show bottom label, plus the top label when timestamp needs to be displayed
        // this will hide the avatar image and message bubble which are not needed for system messages.
        var newHeight: CGFloat = 0
        if (shouldDisplayTimestamp(indexPath)) {
            newHeight = kJSQMessagesCollectionViewCellLabelHeightDefault * 2
        } else {
            newHeight = kJSQMessagesCollectionViewCellLabelHeightDefault
        }
        return CGSizeMake(userMessageCellSize.width, newHeight)
    } else {
        return super.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAtIndexPath: indexPath)
    }
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
    let message = messages[indexPath.item]

    if message.type == MessageType.System {
        return nil
    }

    if message.senderId == senderId{
        return self.outgoingBubbleImageView
    } else {
        return self.incomingBubbleImageView
    }
}

override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
    let message = messages[indexPath.item]

    if message.type == MessageType.System {
        return nil
    }

    if let avatar = avatars[message.senderDisplayName] {
        return avatar
    } else {
        setupAvatarImage(message.senderDisplayName, imageUrl: message.profileImgUrl, incoming: true)
        return avatars[message.senderDisplayName]
    }
}