2009-07-03 88 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)