1

I have a collection view I made in storyboard and a container view with text field constrained to the bottom of the view programmatically. However, when I show the keyboard the collection view stays hidden underneath and I'm not sure how to. I looked and most answers say to use self.view.frame.origin.y in the keyboard notification listener but that's hasn't worked for me.

my keyboard notification code

    @objc func keyboardWillShow(notification: NSNotification) {
        let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
        let duration = ((notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue)!
        
        containerViewbottomAnchor?.constant = -keyboardFrame!.height

        
        UIView.animate(withDuration: duration) {
            self.view.layoutIfNeeded()
        }
        
    }

enter image description here

enter image description here

enter image description here

first image is the normal chat and second is the max view you ca see when the keyboard is shown. the container view contains the textfield and button which I added programmatically. they are within an input accessory view to update their positions when the keyboard comes up.

2 Answers2

0

Have you tried this solution? https://stackoverflow.com/a/57438748/13042987

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)


@objc func handleKeyboardWillShow(notification: Notification) {

        collectionView.scrollToItem(at: IndexPath(row: messagesList.count - 1, section: chatSection), at: .top, animated: false)
}
  • '@objc func handleKeyboardDidShow() { if messages.count > 0 { let indexPath = NSIndexPath(item: messages.count - 1, section: 0) collectionView.scrollToItem(at: indexPath as IndexPath, at: .top, animated: true) } }' yeah this works for taking the user to the bottom but the keyboard still overlaps – Christian Grinling Jun 13 '20 at 16:08
0

I managed to fix it by changing to a UICollectionview controller class