JSQMessages在Show Segue的导航栏下显示

时间:2017-02-15 20:24:58

标签: swift jsqmessagesviewcontroller

我有一个扩展JSQMessagesViewController的ChatViewController。

final class ChatViewController: JSQMessagesViewController {
   ...

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Messages"

        collectionView?.collectionViewLayout.incomingAvatarViewSize = .zero
        collectionView?.collectionViewLayout.outgoingAvatarViewSize = .zero

        automaticallyScrollsToMostRecentMessage = true

        collectionView?.reloadData()
        collectionView?.layoutIfNeeded()
        collectionView?.collectionViewLayout.springinessEnabled = true

        // REMOVE the attachment button for now
        self.inputToolbar.contentView?.leftBarButtonItem = nil
        self.inputToolbar.contentView?.textView?.placeHolder = "Compose a message to your driver"
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        observeMessages()
    }

    private func observeMessages() {
        messageRef = channelRef!.child("messages")
        let messageQuery = messageRef.queryLimited(toLast:25)
        newMessageRefHandle = messageQuery.observe(.childAdded, with: { (snapshot) -> Void in
            let messageData = snapshot.value as! Dictionary<String, String>
            if let id = messageData["senderId"] as String!, let name = messageData["senderName"] as String!, let text = messageData["text"] as String!, text.characters.count > 0 {
                self.addMessage(withId: id, name: name, text: text)
                self.finishReceivingMessage()
            } else {
                print("Error! Could not decode message data")
            }
        })
    }

 }

此视图控制器由具有导航控制器的push segue调用。在我的故事板中,我有一个来自条形项按钮的简单segue,它将推送到View Controller,并将ChatViewController设置为自定义类。在故事板中,推断出导航栏(显示在顶部)。

messages over bar

然而,当我进入ChatViewController时,消息显示在导航栏下方。单击消息文本框后,消息将正确定位。如何解决此问题,以便用户无需单击文本框?是否缺少正确布置新消息的命令?

1 个答案:

答案 0 :(得分:2)

实际上已经解决了herehere这个问题。您只需设置顶部插图即可计算条形高度。 Jesse Squires实际上为这些名为topContentAdditionalInset的案件成了特别成员,您可以这样使用:

override func viewDidLoad() {
    super.viewDidLoad()

    self.topContentAdditionalInset = myBarsHeight   // in CGFloat
}