简体   繁体   中英

How to move up collection view controller when keyboard appears?

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

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM