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;
Jakubknejzlik
source share