仅当用户拉下表格时才显示search栏

我有一个表格查看与顶部的search栏。 我的要求是当有人打开页面时不显示search栏,但是当有人滑动桌面时,search栏应该是可见的。

在控制器的viewDidAppear:方法中,设置表视图的contentOffset属性(在UIScrollView中)以隐藏search栏。

 - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; self.tableView.contentOffset = CGPointMake(0, SEARCH_BAR_HEIGHT); } 

与穆拉特的答案相关 ,这里有一个更便携和正确的版本,将消除视图加载animation偏移(它假定search栏有一个名为searchBar的sockets属性):

 - (void)viewWillAppear:(BOOL)animated { self.tableView.contentOffset = CGPointMake(0, self.searchBar.frame.size.height); } 

更新:

为了适应点击索引部分中的search图标,需要执行以下方法,恢复内容偏移量:

 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { index--; if (index < 0) { [tableView setContentOffset:CGPointMake(0.0, -tableView.contentInset.top)]; return NSNotFound; } return index; }