简体   繁体   English

UITableView-调整框架大小并滚动到行

[英]UITableView - resizing frame and scrolling to row

I have a UITableView and I need to show a picker, so I want to shorten the table's frame's height and at the same time scroll to the row to keep it visible. 我有一个UITableView ,我需要显示一个选择器,因此我想缩短表框架的高度,同时滚动到该行以使其可见。

I do this: add a bottom margin of 400 pixels (by setting the bottom constraint distance to 400) and scroll to the row 我这样做:添加400像素的底部边距(通过将底部约束距离设置为400)并滚动到该行

bottomConstraint.constant = 400
tableview.scrollToRow(at: currentIndexPath, at: .bottom, animated: true)

Both commands work by themselves (I tested). 这两个命令都是自己工作的(我测试过)。 But the result is that the table is shortened and the selected row does not appear on sight (for instance if the row is the last row). 但是结果是缩短了表格,并且所选行没有出现在视线中(例如,如果该行是最后一行)。

Apparently what is happening is that the scroll is processed before the constraint is actually updated, so if the row is visible before the constraint is changed, it does not scroll when the table is shortened. 显然发生的事情是在实际更新约束之前已处理滚动,因此,如果在更改约束之前该行可见,则在缩短表时不会滚动。

Any ideas? 有任何想法吗?

You need to call self.view.layoutIfNeeded() after you set the constraint. 设置约束后,需要调用self.view.layoutIfNeeded() The other option is that you can animate that constraint change and call tableView.scroll... in the completion handler, though the way you animate constraints is to set the constant and then call layoutIfNeeded in the animation block anyway, so it's essentially the same thing aside from the visual and timing aspect. 另一个选择是您可以设置约束变化的动画,并在完成处理程序中调用tableView.scroll... ,尽管设置约束的动画方法是设置常量,然后无论如何layoutIfNeeded在动画块中调用layoutIfNeeded ,所以本质上是相同的除了视觉和时间方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM