farhan
farhan

Reputation: 233

Why is it that when i call setContentOffset for scroll to top tableView, sometimes it goes half way or so?

Trying to scroll tableview back to top but when i call this:

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

Sometimes it works and scrolls all the way to the top, but it also oddly doesn't go all the way up.

Upvotes: 1

Views: 299

Answers (1)

KAR
KAR

Reputation: 3361

try one of these,

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

Or

UITableView is a subclass of UIScrollView, so you can also use:

[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

Or

[mainTableView setContentOffset:CGPointZero animated:YES];

Or

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

edit your

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController

Like below,

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    static UIViewController *previousController = nil;
    if (previousController == viewController) {
        // the same tab was tapped a second time
        if ([viewController respondsToSelector:@selector(scrollToTop)]) {
            [viewController scrollToTop];
        }
    }
    previousController = viewController;
}

Hope this will help you.

Upvotes: 1

Related Questions