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

2

6

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.

Blossom answered 26/5, 2011 at 19:17 Comment(0)
B
16

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;
Bursitis answered 11/6, 2011 at 21:40 Comment(3)
the line of code you have for viewDidLoad should be 'savedContentOffsetY', right? Otherwise this code looks great, I will accept it as the answer as soon as I get to try it out.Blossom
Good find, Kamchatka. Looks like 'performSelector' enables you to get around some timing challenges with animation that you have with viewWillAppearBlossom
Thanks for catching the typo, fixed now.Bursitis
R
1

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;
Reta answered 2/9, 2015 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.