Pagorn
Pagorn

Reputation: 612

How to click Cancel button one time in SearchBar when it's not focus

I add the SearchBar view in NavigationBar.

[self.navigationController.view addSubview:self.searchBar];

When I am searching...

enter image description here

The search is complete...

enter image description here

If I want to cancel (remove) SearchBar, I must tap to Cancel button TWO time for remove SearchBar. (one time for focus SearchBar and one time for tap Cancel).

How can I just one tap to Cancel like (x) button (clear text button) in SearchBar? or How can SearchBar always focused? (on this way I can tap Cancel for close SearchBar only one tap)

Upvotes: 1

Views: 1025

Answers (4)

Wonseok Jang
Wonseok Jang

Reputation: 1

When search bar has resigned from first responder, cancel button is disabled.

So you should enable the cancel button manually.

Try this:

[searchBar resignFirstResponder];

[(UIButton *)[searchBar valueForKey:@"_cancelButton"] setEnabled:YES];

Upvotes: 0

Sheereen S
Sheereen S

Reputation: 1236

Don't dismiss your keyboard after search

remove this line [searchBar resignFirstResponder]

It will work

enter image description here

Upvotes: 1

Abhinandan Pratap
Abhinandan Pratap

Reputation: 2148

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    controller.active = YES;

    [self.view addSubview:controller.searchBar];
    [self.view bringSubviewToFront:controller.searchBar];
}

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView  {

    tableView.frame = self.archiveListTblView.frame;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    controller.active=NO;

    [self.view addSubview:controller.searchBar];
}

Upvotes: 0

user5917312
user5917312

Reputation:

Implement the search bar delegate method as :

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
        self.navigationController.navigationBarHidden = NO;
        [mySearchBar setAlpha:0.0];
    }

Hope you have got the idea by doing this. Also you can add it directly to navigation controller, itself, if you want & then play with Showing/ hiding the searchBar alone.

Upvotes: 0

Related Questions