调整UITableView的高度,并保持滚动底部
我试图通过动画调整tableview的高度,它通过动画tableview的frame.size.height工作正常。调整UITableView的高度,并保持滚动底部
的问题是,我有一个tableview中是200像素高度,滚动至底部,我想动画此为100px,我运行一个简单的
[UIView animateWithDuration:0.245f animations:^{
CGRect frame = tableview.frame;
frame.size.height = 100.f;
tableview.frame = frame;
}];
这工作得很好,但之后,我调整它它不再滚动到tableview的底部。我希望桌面视图始终在动画的底部滚动。我尝试了目前存在的很多事情就像调用
[tablview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
右侧前/后,我开始调整大小的动画,但我没有设法让他们同步的100%。有没有办法调整tableview的大小,而tableview的底部显示scrollview的最后一个元素。
我发现我的问题的解决方案,也很可能是一个更好的解决办法,但其实这工作得很好:)
开始之前,我的动画我看到,如果contentSize.height是比目标高度大,如果所以我以下:
if (mMessages.contentSize.height > 150.f)
{
CGFloat expandedOffsetY = 52.f;
CGFloat collapsedBottomOffset = (150.f + 18.f);
CGFloat expandedBottomOffset = (MIN(320.f, mMessages.contentSize.height) + expandedOffsetY);
tableFrame.origin.y = (collapsedBottomOffset - expandedBottomOffset) + expandedOffsetY;
}
else
{
tableFrame.origin.y = 18.f;
tableFrame.size.height = 150.f;
}
这将使在的tableview负origin.y,然后我已包裹着夹子视图的“父”的UIView内的tableview = YES。
然后我有一个“完成”动画块,“复位”到目标值。
CGRect tableFrame = mMessages.frame;
tableFrame.origin.y = 18.f;
tableFrame.size.height = 150.f;
mMessages.frame = tableFrame;
if (mMessages.contentSize.height > tableFrame.size.height)
{
float contentOffsetY = mMessages.contentSize.height - tableFrame.size.height;
mMessages.contentOffset = CGPointMake(0.0f, contentOffsetY);
}
尝试在为框架设置动画时更新表视图的contentOffset。
[UIView animateWithDuration:0.245f animations:^{
CGRect frame = tableview.frame;
frame.size.height = 100.f;
tableview.frame = frame;
float contentOffsetY = tableview.contentSize - tableview.frame.size.height;
tableview.contentOffset = CGPointMake(0, contentOffsetY);
}];
感谢您的建议,我害怕它看起来不像我想要它,做了一点挖掘后,它看起来像设置contentOffsetY而动画高度给出了一些奇怪的行为(抱歉,但很难解释)。但结果并不是在调整大小时,tableview的底部会向上移动。 – 2012-04-27 07:18:45
[UIView animateWithDuration:0.245f animations:^{
CGRect frame = tableview.frame;
frame.size.height = 100.f;
tableview.frame = frame;
} completion:^(BOOL finished) {
// Find your last indexpath
[tablview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
} ];
我希望Scrollanimation与调整大小动画不发生后,但谢谢:) – 2012-04-27 07:19:00
滚动的tableview在动画块底部是为我工作,有一个小转折:当我降低了的tableView我叫scrollToIndexPath没有动画的身高,当我展开的tableView我叫带动画的scrollToIndexPath。
UIView.animate(withDuration: 0.25) { [weak self] in
guard let strongSelf = self
else { return }
// ...
// updateConstraints
// ...
strongSelf.view.layoutIfNeeded()
if reduceSize {
tableView.scrollToLastRow(animated: false)
} else {
tableView.scrollToLastRow(animated: true)
}
}
扩展的UITableView
extension UITableView {
func scrollToLastRow(animated: Bool) {
let lastSection = numberOfSections-1
let lastRow = numberOfRows(inSection: lastSection)-1
let lastIndexPath = IndexPath(row: lastRow, section: lastSection)
scrollToRow(at: lastIndexPath, at: .bottom, animated: animated)
}
}
使用[UIView的animateWithDuration:动画:完成:]呢? – onnoweb 2012-04-25 20:00:08
我想滚动发生与调整大小动画没有后,但谢谢:) – 2012-04-27 07:16:34