Ios 集合视图高度
当键盘弹出时,我使用高度约束来降低集合视图的高度Ios 集合视图高度,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,当键盘弹出时,我使用高度约束来降低集合视图的高度 @objc func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { self.collectionViewHeightConstraint.cons
@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
}
}
如何使集合视图显示前半部分而不是后半部分
尝试在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)
}
}
我认为作者的意思是显示集合视图的顶部,而您正在检索最后一部分的索引。但是逻辑基本上是一样的这是我的错。。我想请你下楼。答案已编辑