Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS UITableView contentOffSet no longer hides header on return from pushed detail view

I'm using the code below in the root view controller to hide the UITableView's header (header has a UISearchbar in it). It works when the app starts up and displays the tableView.. However afterwards, when a row is selected, the detail view is pushed, and the user pops the detail view, the uitableview header is now visible in the root view, which is not what I expected.

Here's the relevant functions:

- (void) viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
    self.tableView.contentOffset 
     = CGPointMake(0, self.tableView.tableHeaderView.frame.size.height);
    //it's as if the line above is ignored on when returning from a pushed detail view    
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // theContentOffSet works when I put it hear, but then the user can see it which is not desired    
}

The line '[self.navigationController setNavigationBarHidden:YES animated:animated];' is certainly part of the problem, as without it, the code works and the tableView header is scrolled out of view. However the requirement for the root view is for the navigation bar to be hidden, but showing in the detail view.

like image 806
kris Avatar asked May 26 '11 19:05

kris


2 Answers

After looking around for a while, I found the following post https://devforums.apple.com/message/315519#315519 which solves this issue.

-(void)viewWillAppear:(BOOL)animated
{
    [self performSelector:@selector(updateContentOffset) withObject:nil afterDelay:0.0];
}

- (void)updateContentOffset
{
    self.tableView.contentOffset = CGPointMake(0, savedContentOffsetY);
}

Of course, in viewWillDisappear you can save the content offset as follow:

savedContentOffsetY = self.tableView.contentOffset.y;

And in your viewDidLoad,

savedContentOffsetY = self.tableView.tableHeaderView.frame.size.height;
like image 84
Kamchatka Avatar answered Oct 13 '22 01:10

Kamchatka


I have search and tried a lot but nothing help. Finally the following code helped me out. You can add the code in you viewDidLoad() method:

self.edgesForExtendedLayout = UIRectEdgeNone;
like image 40
Evana Avatar answered Oct 13 '22 01:10

Evana