JSQMessagesViewController上的键盘弹出文本字段/发送按钮消失

时间:2017-07-16 21:33:12

标签: ios swift jsqmessagesviewcontroller

与Github https://github.com/jessesquires/JSQMessagesViewController/issues/2132上的此问题类似 在设备和模拟器中,当您点击在文本字段中输入文本时,键盘弹出,文本字段/发送按钮消失...我无法弄清楚为什么?

编辑:还给出了JSQMessagesViewController现在从昨天开始被弃用...任何替代库? (编辑到编辑:仅供参考,看起来MessageKit将包含一个新的JSQMessages构建https://github.com/MessageKit/MessageKit

我的代码如下:

import UIKit
import JSQMessagesViewController

class MedChatViewController: JSQMessagesViewController {



    var messages = [JSQMessage]()
    var botID = "bot"
    lazy var outgoingBubbleImageView: JSQMessagesBubbleImage = self.setupOutgoingBubble()
    lazy var incomingBubbleImageView: JSQMessagesBubbleImage = self.setupIncomingBubble()

    override func viewDidLoad() {
        super.viewDidLoad()


        // No avatars
        collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
        collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero

    }

    override func viewDidAppear(_ animated: Bool) {
        // messages from someone else
        addMessage(withId: botID, name: "Bot", text: "Hello!  I am here to help you")
        // messages sent from local sender
//        addMessage(withId: senderId, name: "Me", text: "I bet I can run faster than you!")
//        addMessage(withId: senderId, name: "Me", text: "I like to run!")
        // animates the receiving of a new message on the view
        finishReceivingMessage()
    }

      // MARK: Collection view data source (and related) methods

    override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
        return messages[indexPath.item]
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return messages.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
        let message = messages[indexPath.item]

        if message.senderId == senderId {
            cell.textView?.textColor = UIColor.white
        } else {
            cell.textView?.textColor = UIColor.black
        }
        return cell
    }

    override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {
        let message = messages[indexPath.item] // 1
        if message.senderId == senderId { // 2
            return outgoingBubbleImageView
        } else { // 3
            return incomingBubbleImageView
        }
    }

    private func setupOutgoingBubble() -> JSQMessagesBubbleImage {
        let bubbleImageFactory = JSQMessagesBubbleImageFactory()
        return bubbleImageFactory!.outgoingMessagesBubbleImage(with: UIColor.jsq_messageBubbleBlue())
    }

    private func setupIncomingBubble() -> JSQMessagesBubbleImage {
        let bubbleImageFactory = JSQMessagesBubbleImageFactory()
        return bubbleImageFactory!.incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray())
    }




    override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {

        let id = senderId
        let name = "Me"
        let text = text
        // 4
        self.addMessage(withId: id!, name: name, text: text!)

        ApiAIService.sharedInstance.parseUserResponse(fromText: text!, success: { response in
            //print("API.AI response = \(response)")
            if let unwrappedResponse = response?.botResponse {

                 self.addMessage(withId: self.botID, name:  "Bot", text: unwrappedResponse)
                self.finishReceivingMessage() 

                JSQSystemSoundPlayer.jsq_playMessageSentSound() // 4

                self.finishSendingMessage() //TODO:  What does this do?
            }

        }) { (error) in
            print("API.AI error = \(error)")
        }

        // 5
        self.finishReceivingMessage()

        JSQSystemSoundPlayer.jsq_playMessageSentSound() // 4

        finishSendingMessage() // 5
    }

    private func addMessage(withId id: String, name: String, text: String) {
        if let message = JSQMessage(senderId: id, displayName: name, text: text) {
            messages.append(message)
        }
    }

    //TODO: removes avatars?
    override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
        return nil
    }


      // MARK: UITextViewDelegate methods
}

1 个答案:

答案 0 :(得分:0)

解决了这个问题....我只需要在我的viewWillAppear中调用超级:

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