2017-10-09 103 views
1

我有一個表格視圖,我希望它始終在首次出現時滾動到頂部。以下代碼用於正常工作,但它不再在iOS 11中執行任何操作。如何滾動到iOS 11中的表格視圖頂部?滾動到iOS中的桌面視圖頂部11

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

回答

6

您可以使用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

0

看到,因爲它在viewWillAppear,我不知道你是在錯誤的地方這樣做(因爲視圖和表尚未顯示)。試試viewDidAppear

在我身邊,當我將[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated: NO]連接到一個IBAction時,它會盡職地移動到表格中的第一個(或第0個)單元格中(而不需要動畫滾動),因此這個調用在iOS 11中仍然可以正常工作。

+0

是的,它在'-viewDidAppear:'中工作,但我希望表格視圖在出現之前滾動到頂部。這在iOS 11中看起來很糟糕。 –