簡體   English   中英

如何滾動到iOS 7中UITableView的頂部

[英]How to scroll to the top of a UITableView in iOS 7

在iOS 7中,我使用以下代碼滾動到我的UITableView的頂部。 您必須考慮半透明狀態欄和導航欄的重疊。

[tableView 
    setContentOffset:CGPointMake(
        0.0, 
        -tableViewController.topLayoutGuide.length
    ) 
    animated:YES
];

此工作僅您第一次調用它時起作用。 在你第一次調用它時,我的表滾動得比它應該的更遠,顯示出很多空白區域。 此外, UIRefreshControl似乎已凍結。 你必須輕輕推動桌子才能讓它恢復到真正的頂部。 之后,您可以根據需要多次調用此代碼,它的行為與您期望的一樣。

在此輸入圖像描述在此輸入圖像描述

我嘗試過其他方法,但都有問題。 iOS 6的方式在第一次調用時表現得很奇怪。 雖然它在后續調用中沒有大量跳躍,但它們不正確,因為它滾動到表格頂部以下64.0點,因為我們忘記了狀態和導航欄。

[table setContentOffset:CGPointZero animated:YES];

我也嘗試滾動到第一個單元格,但它不會在一次調用中滾動到最頂層。 每次調用它時,它只會向上滾動一頁。

[tableView 
    scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
    atScrollPosition:UITableViewScrollPositionTop 
    animated:YES
];

試試這個:

     NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
    [tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];

在SWIFT

    let top = NSIndexPath(forRow: NSNotFound , inSection: 0)
    tableView.scrollToRowAtIndexPath(top, atScrollPosition: .Bottom, animated: true)

Swift 4.0及以上版本

 let top = NSIndexPath(row: NSNotFound, section: 0)
 tableView.scrollToRow(at: top as IndexPath, at: .bottom, animated: true)

我查看了其他答案,發現以下解決方案對我有用。

-(void) scrollToTop
{
    if ([self numberOfSectionsInTableView:self.tableView] > 0)
    {
        NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
        [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}

希望,它解決了你的問題。

這不是“魔術”。 64偏移用於狀態欄(20點)和導航欄(44點)高度。 如果滾動視圖的偏移量為0並且您還有狀態欄+導航欄,則它將位於這些對象下,您將看到原始內容的-64.0。 在故事板中有一個選項“調整滾動視圖插入”,默認情況下會選中此選項

嘗試:

[tableView
    setContentOffset:CGPointMake(
        tableView.contentOffset.x,
        -tableView.contentInset.top
    )
    animated:YES
];

請試試這個:

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

樣本是這樣的:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:0 animated:YES];

在Objective-C中

[mainTableView setContentOffset:CGPointZero animated:YES];

在斯威夫特:

mainTableView.setContentOffset(CGPointZero, animated:true)

在目標C中

NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
[self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];

在斯威夫特

var scrollIndexPath: NSIndexPath = NSIndexPath(forRow:NSNotFound , inSection: 0)
self.tableview.scrollToRowAtIndexPath(scrollIndexPath, atScrollPosition: UITableViewScrollPosition.top, animated: true)

iOS scrollview表現得有些奇怪。 如您所述,64.0偏移量被“神奇地”添加到視圖層次結構中的第一個滾動視圖“第一次”。 我還沒弄清楚為什么會這樣。 目前我只有一個真正的hackish解決方案:你可以添加一個虛擬滾動作為視圖層次結構中的第一個滾動,高度設置為0.之后,你的解決方案應該照常工作。![在此處輸入圖像描述] [ 1]

截圖: http//imgur.com/LvrbcqG

希望這可以幫助。

其他人都知道為什么會發生這種情況?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM