简体   繁体   中英

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.

I do this: add a bottom margin of 400 pixels (by setting the bottom constraint distance to 400) and scroll to the row

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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