2011-09-30 91 views

回答

22

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

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

使用viewDidAppear:可以导致contentOffset更改显着发生在应用程序的用户身上。使用viewWillAppear:将在向用户显示任何内容之前进行更改。 – Shoerob

+2

你也可以在'viewDidLoad'中做到这一点,一开始就做一次,并且在返回视图时(例如在'UINavigationController'中),仍然记得你在tableView中的位置。 – devios1

4

相关的murat's answer,这里有一个更便携和正确的版本将与动画抵消的观点负载做掉(它假定在搜索栏中有一个名为searchBar出口属性):

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

更新:

为了适应点击索引节中的搜索图标,需要实现以下方法,它将恢复内容偏移:

- (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; 
} 
相关问题