2009-07-03 70 views
25

我有一个UITableView与UISearchBar作为tableViews.tableHeaderView。就像3.0中的新Mail.app,Notes.app等。我想隐藏SearchBar,直到用户在他的视线中拖动它。滚动UITableView,使标题不可见

我的尝试只在tableView中有几个项目时才起作用,所以tableView实际上需要滚动。我称这种现象的loadView:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self._tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 

但似乎苹果不同的方式处理这样的serachbar。拖出搜索栏后,它似乎不再受限于表格单元(在Notes.app中,而不在Mail.app中)。

但也许苹果有一个独特的方法,新的3.0行为,我只是无法找到它?

+0

结帐夫妇为`UIViewController`新的属性。 [https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TransitionGuide/AppearanceCustomization.html#//apple_ref/doc/uid/TP40013174-CH15-SW1](https://developer。 apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TransitionGuide/AppearanceCustomization.html#//apple_ref/doc/uid/TP40013174-CH15-SW1) – 2013-09-13 19:40:21

回答

33

也许你可以尝试这样...

[self.tableView setContentOffset:CGPointMake(0,40)]; 
+0

完美的作品。 – OlivaresF 2012-02-01 03:19:29

+1

每次重新加载表视图时都需要执行此操作。 [self.tableView reloadData]; – Thiru 2013-06-13 16:58:51

+9

当行数少于适合屏幕的总行数时不起作用。该场景的任何已知解决方法? – Zorayr 2015-03-09 05:14:47

25

为我工作了。我使用了以下内容:

[self.tableView setContentOffset:CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height) animated:NO]; 

查询搜索栏的高度。

10

这一个让你完全一样的行为iPod.app:

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 

CGFloat searchBarHeight = CGRectGetHeight([[[self searchDisplayController] searchBar] frame]); 
if ([[self tableView] contentOffset].y < searchBarHeight) 
    [[self tableView] setContentOffset:CGPointMake(0, searchBarHeight)]; 
} 
3

这对我的作品。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tableView.bounces = YES; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    [self.tableView setContentOffset:CGPointMake(0, 44)]; 
} 
-2

我挺喜欢做这种方式:

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // Hide the table view header by default. 
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
} 

这样,你真的不需要担心你的头有多高。它只是工作!

0

我不得不先滚动到顶部,然后setContentOffset0,然后搜索栏是可见的:

self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false) 
self.tableView.setContentOffset(CGPointMake(0, 0), animated: false)