7

I've read multiple posts on this but it's not working properly for me. I'm using the latest 4.2 SDK.

The code I have is

self.tableView.contentOffset = CGPointMake(0.0, 44.0);

This partially works, it moves the search bar up a little bit, but it does not get hidden completely. I've tried increasing the value 44 to something greater and this had no affect what so ever! I'm calling this code in the viewDidLoad method of the table's view controller. Does anyone have any ideas?

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
Darthtong
  • 1,017
  • 4
  • 18
  • 30
  • Can you explain how you laid your tableview out? – Joshua Weinberg Nov 25 '10 at 03:12
  • I'd check out `wantsFullScreenLayout`. If you have a bar at the bottom that will cover part of the table you could either add a footerview 44px high (quick and easy) or you could drop the tableview into a parentview (bit more work but cleaner). – Michael Kernahan Nov 25 '10 at 03:17

5 Answers5

24

Another approach should be...in viewDidLoad call:

self.tableView.contentInset = UIEdgeInsetsMake(-self.searchDisplayController.searchBar.frame.size.height, 0, 0, 0);

and implementing endDragging delegate method:

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    CGPoint offset = self.tableView.contentOffset;

    CGFloat barHeight = self.searchDisplayController.searchBar.frame.size.height;
    if (offset.y <= barHeight/2.0f) {
        self.tableView.contentInset = UIEdgeInsetsZero;
    } else {
        self.tableView.contentInset = UIEdgeInsetsMake(-barHeight, 0, 0, 0);
    }

    self.tableView.contentOffset = offset;
}

setting content is to remove some "flickering"

also if You want searchbar to stick at the top, implement didScroll this way:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
     CGRect sbFrame = self.searchDisplayController.searchBar.frame;
     sbFrame.origin.y = self.tableView.contentOffset.y;
     if (sbFrame.origin.y > 0) {
         sbFrame.origin.y = 0;
     }
     self.searchDisplayController.searchBar.frame = sbFrame;
}

I hope this will help (took me few days to figure out:) )

Cheers!

UPDATE:

As @carbonr noted You should add this line in viewDidLoad since iOS7+

self.edgesForExtendedLayout = UIRectEdgeNone;
JakubKnejzlik
  • 6,363
  • 3
  • 40
  • 41
  • Thanks! It was exactly the "set content offset to remove flickering" part I was missing. – Jens Willy Johannsen Mar 01 '12 at 13:50
  • @GrizzlyNetch thanks a ton !!!! awesome solution, I had been struggling for a long time. What this solution also does is that when the user taps on the top of the iPhone (outside of the app), the table view scrolls to the top but hides the scroll bar, unless he pulls it down after that. – user1046037 Jun 14 '12 at 13:04
  • 1
    @GrizzlyNetch I have a doubt, inside scrollViewDidEndDragging implementation we need to check if the table view being scrolled is self.searchDisplayController.searchResultsTableView or self.tableView. – user1046037 Jun 14 '12 at 14:07
  • @user1046037 good point for optimisation, but i think it will work this way either. But for sure it is better to check which table has been dragged. – JakubKnejzlik Sep 03 '12 at 19:55
  • 3
    The code will behave unexpectedly on iOS 7 and to fix that you need to add this to your `viewDidLoad`. This is new a API `self.edgesForExtendedLayout = UIRectEdgeNone;` – carbonr Mar 31 '14 at 23:18
15
self.tableView.contentOffset = CGPointMake(0.0, 44.0);

The above code does in fact work but it needs to run after the UITableView has finished creating all of its cells. I guess thats another question though.

Darthtong
  • 1,017
  • 4
  • 18
  • 30
  • 1
    how did you make sure the tableview has finished creating all the cells? Can you give me some clues? Thanks! – Brian Jun 05 '15 at 08:55
4

You can set the initial bounds of the table view inside viewDidLoad, so the search bar appears hidden at the beginning.

You have to create the searchBar property and then use following code:

- (void)viewDidLoad
{
    //...
    CGRect bounds = self.tableView.bounds;
    bounds.origin.y = self.tableView.bounds.origin.y + searchBar.bounds.size.height;
    self.tableView.bounds = bounds;
    //...
}
Gerstmann
  • 5,368
  • 6
  • 37
  • 57
mecorre1
  • 111
  • 1
  • 4
0

For others still looking for an updated solution, you can check out my answer over here.

Basically you need to update the contentOffset the first time viewDidLayoutSubviews is called.

Community
  • 1
  • 1
RyanJM
  • 7,028
  • 8
  • 60
  • 90
-1

I also have the same problem like yours. The following code solved my problem.Please add the code in you viewDidLoad() :

self.edgesForExtendedLayout = UIRectEdgeNone;

N:B: I used autoLayout in my project.

Evana
  • 1,754
  • 13
  • 12