[英]MessageKit - Keyboard does not display when i tap on inputBar
Many of the suggestions i've read said to include self.messageInputBar.inputTextView.becomeFirstResponder() in viewDidAppear() which doesn't seem to work for me.我读过的许多建议都说在 viewDidAppear() 中包含 self.messageInputBar.inputTextView.becomeFirstResponder() ,这对我来说似乎不起作用。 I'm able to type 'ABC' from the computer keyboard, but I want to be able to type from the phone keyboard.我可以从计算机键盘键入“ABC”,但我希望能够从电话键盘键入。 Everything else seems to be behaving properly in this class. Below is my code.在这个 class 中,其他一切似乎都正常运行。下面是我的代码。
struct Sender: SenderType {
var senderId: String
var displayName: String
}
struct Message: MessageType {
var sender: SenderType
var messageId: String
var sentDate: Date
var kind: MessageKind
}
class NoteDetailViewController: MessagesViewController, MessagesDataSource, MessagesLayoutDelegate, MessagesDisplayDelegate {
//incoming/outgoing user setup
let currentUser = Sender(senderId: "self", displayName: "Electrician User")
let otherUser = Sender(senderId: "other", displayName: "Admin")
var messages = [MessageType]()
override func viewDidLoad() {
super.viewDidLoad()
//mock data
messages.append(Message(sender: currentUser, messageId: "1", sentDate: Date().addingTimeInterval(-86400), kind: .text("What is the gate code?")))
messages.append(Message(sender: otherUser, messageId: "2", sentDate: Date().addingTimeInterval(-80000), kind: .text("Gate code is #7072")))
messages.append(Message(sender: otherUser, messageId: "3", sentDate: Date().addingTimeInterval(-66400), kind: .text("When will you be there?")))
messages.append(Message(sender: currentUser, messageId: "4", sentDate: Date().addingTimeInterval(-56400), kind: .text("I will be there tomorrow morning at 9")))
messagesCollectionView.messagesDataSource = self
messagesCollectionView.messagesLayoutDelegate = self
messagesCollectionView.messagesDisplayDelegate = self
messageInputBar.delegate = self
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageIncomingAvatarSize(.zero)
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageOutgoingAvatarSize(.zero)
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageIncomingMessageBottomLabelAlignment(LabelAlignment(textAlignment: NSTextAlignment.left, textInsets: .zero))
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageIncomingMessageTopLabelAlignment(LabelAlignment(textAlignment: NSTextAlignment.left, textInsets: .zero))
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageOutgoingMessageBottomLabelAlignment(LabelAlignment(textAlignment: NSTextAlignment.right, textInsets: .zero))
messagesCollectionView.messagesCollectionViewFlowLayout.setMessageOutgoingMessageTopLabelAlignment(LabelAlignment(textAlignment: NSTextAlignment.right, textInsets: .zero))
messagesCollectionView.backgroundColor = UIColor.black
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.messageInputBar.inputTextView.becomeFirstResponder()
}
func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
return isFromCurrentSender(message: message) ? UIColor.systemBlue : UIColor.darkGray
}
func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
return UIColor.white
}
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
return NSAttributedString(
string: MessageKitDateFormatter.shared.string(from: message.sentDate),
attributes: [.font: UIFont.systemFont(ofSize: 12), .foregroundColor: UIColor.gray]) }
func cellTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
return 30
}
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
return messages[indexPath.section]
}
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
return messages.count
}
func currentSender() -> SenderType {
return currentUser
}
}
extension NoteDetailViewController: InputBarAccessoryViewDelegate {
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
print("Sending: \(text)")
}
}
It seems you are using a simulator, you can show it by using this shortcut看来您正在使用模拟器,您可以使用此快捷方式显示它
⌘ + K ⌘ + K
or by navigating to或导航至
I/O -> Keyboard -> Toggle Software Keyboard
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.