[英]scrolling UITableView to bottom in addition to resizing insets
所以我有一个UITableView,当键盘出现时我需要滚动到最后一个单元格。 目前,我正在监听UIKeyboardDidShow / Hide通知,并调整插入大小以使整个tableView在键盘上方,然后尝试向下滚动。 如果在键盘出现时tableView位于最顶部,则可以进行滚动,但如果滚动到列表中间或底部,则它不会一直向下滚动并弄得一团糟(我无法滚动)过去的某个时间点,一般来说有点简陋)。
下面是显示键盘时运行的代码:
func keyboardShown(notification: NSNotification) {
// changes the content insets of the tableView to be above the keyboard
let keyboardSize = notification.userInfo![UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size
var contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize!.height, right: 0.0)
var rate: NSTimeInterval = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey]!.doubleValue
UIView.animateWithDuration(rate, animations: {
self.commentsTableView.contentInset = contentInsets
self.commentsTableView.scrollIndicatorInsets = contentInsets
}, completion: { (value: Bool) in
// scrolls to bottom
self.scrollToBottom()
})
}
这是实际进行滚动的方法:
func scrollToBottom() {
// scrolls to bottom
let rect = CGRect(x: 0.0, y: commentsTableView.contentSize.height - commentsTableView.bounds.size.height, width: commentsTableView.bounds.size.width, height: commentsTableView.bounds.size.height)
commentsTableView.scrollRectToVisible(rect, animated: true)
}
有谁知道实现此目标的更好方法或发现我当前的实现存在任何问题?
我不建议您更改表的contentInset
。 而是更改桌子的高度,例如,当调用您的keyboardShown
函数时,从桌子的高度开始将其高度减小为等于键盘的高度,而当键盘消失时,执行相反的操作。 祝好运!
此方法对我来说效果很好:
NSIndexPath* ipath = [NSIndexPath indexPathForRow: [dataSourceArray count]-1 inSection:0];
[tableView scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated: YES];
您可以使用滚动位置来获得所需的效果,这告诉表格滚动到最后一个单元格而不是特定的CGRect。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.