2011-11-06 71 views
1

我有一個TableView以及底部的搜索按鈕。我想要搜索按鈕使SearchBar在頂部彈出並進入焦點。否則,不應該顯示SearchBar切換iOS中的搜索欄

將搜索欄放在TableView上方很容易,但是有沒有辦法用動畫隱藏/顯示它?

謝謝。

回答

1

您是否嘗試過使用-[UITableView scrollRectToVisible:animated:]方法?我認爲UISearchBar視圖通常只是表格中的標題視圖,因此您應該可以請求表格視圖向上滾動以顯示搜索欄。

+0

這樣的作品,但搜索欄仍然是默認可見,因爲它已被指定爲'tableView.tableHeaderView'。此外,它的笨拙,因爲tableview必須一直滾動到頂部才能訪問'UISearchBar'。我正在尋找一種方法來顯示「UISearchbar」,直到搜索按鈕被點擊。現在,一個'UISearchDisplayController'接近我想要的,儘管它最初並沒有隱藏'UISearchBar'。 – user1032657

3

這適用於我。希望能幫助到你。

在您的viewWillAppear中調用[self hideSearchBar](這會最初隱藏搜索欄)。

你的搜索按鈕應具有以下作用:

- (IBAction)searchIconButtonClicked { 
    if (self.searchDisplayController.isActive || (self.tableView.contentOffset.y < 44)) { 
     if (self.searchDisplayController.isActive) { 
      self.searchDisplayController.searchBar.text = nil; 
      [self.searchDisplayController setActive:NO animated:YES]; 
      [tableView reloadData]; 
     } 
     [self hideSearchBar]; 
    } else { 
     [self.patientTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]; 
    } 
} 

- (void)hideSearchBar { 
    //NSLog(@"Hiding SearchBar");   
    [self.tableView setContentOffset:CGPointMake(0,44)]; 
} 
+0

感謝兄弟。像我想要的那樣工作 – khalil