0

Many of the suggestions i've read said to include self.messageInputBar.inputTextView.becomeFirstResponder() in viewDidAppear() which doesn't seem to work for me. I'm able to type 'ABC' from the computer keyboard, but I want to be able to type from the phone keyboard. Everything else seems to be behaving properly in this class. Below is my code.

enter image description here

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)")
        
    }
}

1 Answers1

0

It seems you are using a simulator, you can show it by using this shortcut

+ K

or by navigating to

I/O -> Keyboard -> Toggle Software Keyboard

JohnG
  • 75
  • 10