チャット用のライブラリMessageKitを使っているんですが、
送信ボタンを押しても何の反応もないです。エラーもありません。
swift
1class ChatTestViewController: MessagesViewController { 2// let sender = Sender(id: "123", displayName: "自分") 3 var messageList: [MockMessage] = [] 4 5 lazy var formatter: DateFormatter = { 6 let formatter = DateFormatter() 7 formatter.dateStyle = .medium 8 return formatter 9 }() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 DispatchQueue.main.async { 15 // messageListにメッセージの配列をいれて 16 self.messageList = self.getMessages() 17 // messagesCollectionViewをリロードして 18 self.messagesCollectionView.reloadData() 19 // 一番下までスクロールする 20 self.messagesCollectionView.scrollToBottom() 21 } 22 23 messagesCollectionView.messagesDataSource = self 24 messagesCollectionView.messagesLayoutDelegate = self 25 messagesCollectionView.messagesDisplayDelegate = self 26 messagesCollectionView.messageCellDelegate = self 27 28 messageInputBar.delegate = self 29 messageInputBar.sendButton.tintColor = UIColor.lightGray 30 31 // メッセージ入力時に一番下までスクロール 32 scrollsToBottomOnKeyboardBeginsEditing = true // default false 33 maintainPositionOnKeyboardFrameChanged = true // default false 34 } 35 36 // サンプル用に適当なメッセージ 37 func getMessages() -> [MockMessage] { 38 return [ 39 createMessage(text: "あ"), 40 createMessage(text: "い"), 41 createMessage(text: "う"), 42 createMessage(text: "え"), 43 createMessage(text: "お"), 44 createMessage(text: "か"), 45 createMessage(text: "き"), 46 createMessage(text: "すせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん"), 47 ] 48 } 49 50 func createMessage(text: String) -> MockMessage { 51 let attributedText = NSAttributedString(string: text, attributes: [.font: UIFont.systemFont(ofSize: 15), 52 .foregroundColor: UIColor.black]) 53 return MockMessage(attributedText: attributedText, sender: otherSender(), messageId: UUID().uuidString, date: Date()) 54 } 55 56 override func didReceiveMemoryWarning() { 57 super.didReceiveMemoryWarning() 58 } 59 60} 61 62extension ChatTestViewController: MessagesDataSource { 63 func currentSender() -> SenderType { 64 return Sender(id: "123", displayName: "自分") 65 } 66 67 func otherSender() -> Sender { 68 return Sender(id: "456", displayName: "知らない人") 69 } 70 71 func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int { 72 return messageList.count 73 } 74 75 func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType { 76 return messageList[indexPath.section] 77 } 78 79 // メッセージの上に文字を表示 80 func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { 81 if indexPath.section % 3 == 0 { 82 return NSAttributedString( 83 string: MessageKitDateFormatter.shared.string(from: message.sentDate), 84 attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10), 85 NSAttributedString.Key.foregroundColor: UIColor.darkGray] 86 ) 87 } 88 return nil 89 } 90 91 // メッセージの上に文字を表示(名前) 92 func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { 93 let name = message.sender.displayName 94 return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)]) 95 } 96 97 // メッセージの下に文字を表示(日付) 98 func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { 99 let dateString = formatter.string(from: message.sentDate) 100 return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)]) 101 } 102} 103 104// メッセージのdelegate 105extension ChatTestViewController: MessagesDisplayDelegate { 106 107 // メッセージの色を変更(デフォルトは自分:白、相手:黒) 108 func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor { 109 return isFromCurrentSender(message: message) ? .white : .darkText 110 } 111 112 // メッセージの背景色を変更している(デフォルトは自分:緑、相手:グレー) 113 func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor { 114 return isFromCurrentSender(message: message) ? 115 UIColor(red: 69/255, green: 193/255, blue: 89/255, alpha: 1) : 116 UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1) 117 } 118 119 // メッセージの枠にしっぽを付ける 120 func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle { 121 let corner: MessageStyle.TailCorner = isFromCurrentSender(message: message) ? .bottomRight : .bottomLeft 122 return .bubbleTail(corner, .curved) 123 } 124 125 // アイコンをセット 126 func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) { 127 // message.sender.displayNameとかで送信者の名前を取得できるので 128 // そこからイニシャルを生成するとよい 129 let avatar = Avatar(initials: "人") 130 avatarView.set(avatar: avatar) 131 } 132} 133 134// 各ラベルの高さを設定(デフォルト0なので必須) 135extension ChatTestViewController: MessagesLayoutDelegate { 136 137 func cellTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat { 138 if indexPath.section % 3 == 0 { return 10 } 139 return 0 140 } 141 142 func messageTopLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat { 143 return 16 144 } 145 146 func messageBottomLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat { 147 return 16 148 } 149} 150 151extension ChatTestViewController: MessageCellDelegate { 152 // メッセージをタップした時の挙動 153 func didTapMessage(in cell: MessageCollectionViewCell) { 154 print("Message tapped") 155 } 156} 157 158extension ChatTestViewController: MessageInputBarDelegate { 159 // メッセージ送信ボタンをタップした時の挙動 160 func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) { 161 print("タップした") 162 for component in inputBar.inputTextView.components { 163 if let image = component as? UIImage { 164 165 let imageMessage = MockMessage(image: image, sender: currentSender() as! Sender, messageId: UUID().uuidString, date: Date()) 166 messageList.append(imageMessage) 167 messagesCollectionView.insertSections([messageList.count - 1]) 168 169 } else if let text = component as? String { 170 171 let attributedText = NSAttributedString(string: text, attributes: [.font: UIFont.systemFont(ofSize: 15), 172 .foregroundColor: UIColor.white]) 173 let message = MockMessage(attributedText: attributedText, sender: currentSender() as! Sender, messageId: UUID().uuidString, date: Date()) 174 messageList.append(message) 175 messagesCollectionView.insertSections([messageList.count - 1]) 176 } 177 } 178 inputBar.inputTextView.text = String() 179 messagesCollectionView.scrollToBottom() 180 } 181} 182
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。