滚动到iOS 11中的表格视图顶部

Ber*_*lue 3 uitableview uiscrollview ios11

我有一个表视图,我希望它在第一次出现时始终滚动到顶部.以下代码用于工作正常但在iOS 11中不再执行任何操作.如何在iOS 11中滚动到表格视图的顶部?

- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];    
}
Run Code Online (Sandbox Code Playgroud)

Jac*_*ack 14

您可以使用 scrollToRowAtIndexPath方法 -

ObjC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:true];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:true];
}
Run Code Online (Sandbox Code Playgroud)

SWIFT 4

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true)
    }
Run Code Online (Sandbox Code Playgroud)

您还可以通过设置indexpath各种行和节值来设置自定义行.