文本 Field/Send 按钮在 JSQMessagesViewController 上的键盘弹出时消失
Text Field/Send button disappears on keyboard pop on JSQMessagesViewController
类似于 Github https://github.com/jessesquires/JSQMessagesViewController/issues/2132 上的这个问题
在设备和模拟器中,当您点击以在文本字段中输入文本时,键盘弹出并且 textfield/send 按钮消失...我不明白为什么?
编辑:还考虑到 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
}
解决了这个....我只需要在我的 viewWillAppear
中调用 super:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
}
类似于 Github https://github.com/jessesquires/JSQMessagesViewController/issues/2132 上的这个问题 在设备和模拟器中,当您点击以在文本字段中输入文本时,键盘弹出并且 textfield/send 按钮消失...我不明白为什么?
编辑:还考虑到 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
}
解决了这个....我只需要在我的 viewWillAppear
中调用 super:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
}