Hide UISearchbar as header inside UITableview when cancel is touched

被刻印的时光 ゝ 提交于 2019-12-24 11:27:03

问题


I have integrated a UISearchDisplayController into my controller's xib. The basic functionality I'm going for is to have the search bar in the header of the table, and a button in the navigation bar that shows the search display.

What I'm stuck on: When the cancel is touched, I want to make the searchbar invisible (keep the table so index 0 is show at top, not the search bar), but it stays visible and i'm not sure why (see 3rd image). Any ideas how to always keep the searchbar hidden when the cancel button is touched (see image 1).

What I've tried:

  1. Setting the tableview contentOffset to 44. Which works initially.
  2. Calling [tableview scrollToRowAtIndexPath:....], which doesn't seem to do anything.

回答1:


Try this:

- (void)viewWillAppeared:(BOOL)animated
{
        [self hidesSeachBar];
        [super viewWillAppeared:animated];
}

- (void)hidesSearchBar
{
        CGSize searchSize = self.searchDisplayController.searchBar.bounds.size;
        //not complete
        [self.tableView setContentOffset:CGPointMake(0, searchSize.height)];
}

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    //Your code

    [self.tableView reloadData];
    [self hidesSearchBar];
}



回答2:


If you've declared your search bar as a property like that

@property(nonatomic, retain)UISearchBar *searchBar;

you can set

tableView.tableheader = nil;

and set it back

tableView.tableHeader = searchBar;

when you'll need it again.




回答3:


Setting the content offset is the way to hide it, and you need to do that when the search is ended. I'm using something like this:

- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController*)controller
{
    [self hideSearchBar];
}


- (void) hideSearchBar
{
    self.table.contentOffset = CGPointMake( 0, self.searchBar.frame.size.height );  
}



回答4:


to hide the search bar of a UISearchDisplayController you have to set it to inactive. This will reshow the navigation bar as well

In your case, you must ensure that you adopt the UISearchBarDelegate protocol and then:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchDisplayController setActive:NO animated:YES];
}



回答5:


To hide search bar on cancel, you need to call UISearchBarDelegate method:

(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    if(searchTask)[searchTask cancel];

    [self.searchResults removeAllObjects];
    [self.searchDisplayController.searchResultsTableView reloadData];

   //reload you table here
}

Thanks



来源:https://stackoverflow.com/questions/3380727/hide-uisearchbar-as-header-inside-uitableview-when-cancel-is-touched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!