当键盘出现时,我添加了 UIVCollectionView 的 contentSize,希望它能够向上滚动,但是不起作用,他仍然没有增加滚动范围。
我设置 contentSize self.collectionView.contentSize = CGSizeMake(0, self.view.frame.size.height + deltaY)
。我在模拟器 5s Xcode7.1 上运行应用程序。contentSize 为 (0.0, 832.0)。为什么 UIcollectionView 不能向上滚动。
这是方法keyboardWillShow:
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
let keyboardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let keyboardBoundsRect = self.view.convertRect(keyboardBounds, toView: nil)
let deltaY = keyboardBoundsRect.size.height + finishViewHeight
print(self.collectionContentSize)
//here I set contentSize
self.collectionView.contentSize = CGSizeMake(0, self.view.frame.size.height + deltaY)
print(self.collectionView.contentSize)
let animations: (()->Void) = {
self.finishView!.transform = CGAffineTransformMakeTranslation(0, -deltaY)
}
if duration > 0 {
let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue << 16))
UIView.animateWithDuration(duration, delay: 0, options:options, animations: animations, completion: nil)
} else {
animations()
}
}
}