我有一个表格 View ,我希望它在第一次出现时总是滚动到顶部。以下代码过去可以正常工作,但在 iOS 11 中不再起作用。如何在 iOS 11 中滚动到表格 View 的顶部?
- (void)viewWillAppear:(BOOL)animated
{
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}
最佳答案
您可以使用 scrollToRowAtIndexPath
方法
作为 -
ObjC
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:true];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:true];
}
SWIFT 4
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true)
}
您还可以在
indexpath
中设置自定义行通过放置各种行和节值。
关于uitableview - 在 iOS 11 中滚动到表格 View 的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46637998/