how to show cancel button of searchbar?

2020-07-03 06:46发布

I want to hide the Navigation Bar of my page when the user start editing on the searchbar, I also want to show a cancel button.
It is done but my cancel button is not accessible when I bring up the UISearchBar Thanks to All.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.navigationController.navigationBar.hidden=TRUE;
    CGRect r=self.view.frame;
    r.origin.y=-44;
    r.size.height+=44;

    self.view.frame=r;

    searchBar.showsCancelButton=TRUE;

}

6条回答
贪生不怕死
2楼-- · 2020-07-03 06:55

You need to use UISearchDisplayController, that will hide navigation bar automatically for you. Here is the code,

-(void) createSearchBar
{
     _searchBar          = [[SearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
     _searchBar.delegate = self;
     [self.view addSubview:_searchBar];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;
    searchDisplayController.searchResultsTableView.sectionHeaderHeight = 10;
    searchDisplayController.searchResultsTableView.sectionFooterHeight = 0;
    [searchDisplayController.searchResultsTableView setSeparatorColor:[Color whiteColor]];  
}

You will have to implement protocols UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDelegate, UITableViewDataSource in .h file.

also _searchBar & searchDisplayController are variables defined in .h file.

查看更多
▲ chillily
3楼-- · 2020-07-03 06:59

Swift 4:

Below 2 lines of code worked for me to show cancel button:

searchBar.setShowsCancelButton(true, animated: true)
searchBar.showsCancelButton = true

Calling inside searchBarTextDidBeginEditing() method will show cancel button after clicking on search bar.

查看更多
Ridiculous、
4楼-- · 2020-07-03 07:02

Objective C

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.navigationController.navigationBar.hidden = TRUE;
    CGRect r = self.view.frame;
    r.origin.y = -44;
    r.size.height += 44;
    self.view.frame = r;

    [searchBar setShowsCancelButton:YES animated:YES];
}


-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}

Swift

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    self.navigationController.navigationBar.hidden = true
    var r = self.view.frame
    r.origin.y = -44
    r.size.height += 44

    self.view.frame = r
    searchBar.setShowsCancelButton(true, animated: true)
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(false, animated: true)
}
查看更多
该账号已被封号
5楼-- · 2020-07-03 07:04

Use this code to show/Hide the cancel Button in SearchBar

As user will start the Editing in SearchBar SHow the Cancel Button.

Below methods will only be accessible if you have set the delegate of SearchBar properly.

 -(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar
  {
    //This'll Show The cancelButton with Animation  
    [searchBar setShowsCancelButton:YES animated:YES];
    //remaining Code'll go here
  }

Hide The CancelButton as User Click on Cancel Button

 - (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
 {
        //This'll Hide The cancelButton with Animation  
      [searchBar setShowsCancelButton:NO animated:YES];
       //remaining Code'll go here
 }
查看更多
三岁会撩人
6楼-- · 2020-07-03 07:14

Use this code

searchBar.showsCancelButton=YES;
查看更多
Summer. ? 凉城
7楼-- · 2020-07-03 07:14

Use this code for showing cancel button. It will show button when you will enter text in searchBar.

-(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:YES];
}
查看更多
登录 后发表回答