Hide default UITableView search bar when in navigation controller

I read a few posts about this, but it does not work properly for me. I am 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 a bit, but is not completely hidden. I tried to increase the value of 44 to something more, and this did not affect the fact that it was! I call this code in the viewDidLoad method of a table view controller. Does anyone have any idea?

+6
ios objective-c iphone
source share
5 answers
 self.tableView.contentOffset = CGPointMake(0.0, 44.0); 

The above code does work, but it should run after the UITableView has completed creating all of its cells. I guess this is another question.

+14
source share

Another approach should be ... in viewDidLoad call:

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

and implement the endDragging delegation 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; } 

content customization - remove some “flickering”

also, if you want the search bar to stick to the top, implement didScroll as follows:

 -(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; } 

Hope this helps (it took me a few days to figure it out :))

Hooray!

UPDATE:

As @carbonr pointed out. You must add this line to viewDidLoad, since iOS7 +

 self.edgesForExtendedLayout = UIRectEdgeNone; 
+24
source share

You can set the initial boundaries of the table view inside viewDidLoad , so the search bar will be hidden at the beginning.

You need to create the searchBar property and then use the 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; //... } 
+4
source share

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

Basically, you need to update the contentOffset first time viewDidLayoutSubviews call viewDidLayoutSubviews .

0
source share

I also have the same problem as yours. The following code solved my problem. Please add the code to viewDidLoad ():

 self.edgesForExtendedLayout = UIRectEdgeNone; 

N: B: I used autoLayout in my project.

-one
source share

All Articles