0

In my iOS app, I would like to have a right side search button in my UINavigationController, and when a user touches the button, the UISearchBar above my UITableView is shown.

I would like the UISearchBar hidden when the view loads, and then hidden again when the user clicks the Cancel button in the UISearchDisplayController.

I've searched everywhere and cannot find an example. Help?

4

1 回答 1

1

这里有一个很好的示例项目。关键点是:

1.在显示视图之前隐藏搜索栏:

-(void)viewWillAppear:(BOOL)animated {
    [self hideSearchBar];        
}

-(void)hideSearchBar {
   CGRect newBounds = self.tableView.bounds;
   newBounds.origin.y = newBounds.origin.y + self.searchBar.bounds.size.height;
   self.tableView.bounds = newBounds;
}

2.在搜索按钮的操作目标上,显示搜索栏

 // make the search bar visible
 // code example from https://github.com/versluis/Table-Seach-2013 to deal with iOS 7 behavior

-(IBAction)displaySearchBar:(id)sender {

    [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

    NSTimeInterval delay;
    if (self.tableView.contentOffset.y >1000) delay = 0.4;
    else delay = 0.1;
    [self performSelector:@selector(activateSearch) withObject:nil afterDelay:delay];

}

- (void)activateSearch
{
    [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
    [self.searchBar becomeFirstResponder];

}

3.最后,点击取消时隐藏SearchBar

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self hideSearchBar];
}
于 2013-10-31T15:02:47.970 回答