MessageKit - 当我点击 inputBar 时,键盘不显示

Posted

技术标签:

【中文标题】MessageKit - 当我点击 inputBar 时,键盘不显示【英文标题】:MessageKit - Keyboard does not display when i tap on inputBar 【发布时间】:2021-08-18 17:36:12 【问题描述】:

我读过的许多建议都说在 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)")
        
    

【问题讨论】:

你在用模拟器吗?如果是这样,您可以按command + 'K' 显示它 【参考方案1】:

你好像用的是模拟器,你可以用这个快捷方式展示一下

+ K

或通过导航到

I/O -> Keyboard -> Toggle Software Keyboard

【讨论】:

谢谢 - 键盘会出现在实际设备上吗? @user9650660 是的,会的。

以上是关于MessageKit - 当我点击 inputBar 时,键盘不显示的主要内容,如果未能解决你的问题,请参考以下文章

MessageKit:将手势识别器添加到 messageCollectionView 和 cellDelegate 不起作用

委托和数据源在 MessageKit Swift IOS 中不起作用

MessageKit 不显示消息输入栏 Swift 5

如何使用 MessageKit 创建自定义单元格?

消息线程`MessageKit`swift的左对齐

在 MessageKit 中 configureAvatarView 无法正常工作