我正在使用高度限制来降低键盘弹出时集合视图的高度。
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.collectionViewHeightConstraint.constant = 250 - keyboardSize.height/2
self.addButtonConstraint.constant = keyboardSize.height+20
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.collectionViewHeightConstraint.constant = 250
self.addButtonConstraint.constant = 20
}
}
如何使集合视图显示的是上半部而不是后半部?
答案 0 :(得分:1)
尝试在keyboardWillShow方法中调用它:
collectionView.scrollToTop(false)
扩展名
extension UIScrollView {
func scrollToTop(_ animated: Bool) {
var topContentOffset: CGPoint
if #available(iOS 11.0, *) {
topContentOffset = CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)
} else {
topContentOffset = CGPoint(x: -contentInset.left, y: -contentInset.top)
}
setContentOffset(topContentOffset, animated: animated)
}
}