我读过的许多建议都说在 viewDidAppear() 中包含 self.messageInputBar.inputTextView.becomeFirstResponder() ,这似乎对我不起作用。我可以从电脑键盘输入“ABC”,但我希望能够从手机键盘输入。在这堂课中,其他一切似乎都表现得很好。下面是我的代码。
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)")
}
}