简体   繁体   中英

iOS 7 Search Bar - Cancel button doesn't work

I have a UISearchBar in iOS 6 app and it works perfectly, but in iOS 7 the cancel button and the clear button don't work and I can't return back. It's a big problem in my app and I need to solve it.

My code is:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{
    // -- iOS 7 Hack

    if (!SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        controller.searchResultsTableView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64);
        [controller.searchContentsController.view setNeedsLayout];
    }

    [self filterContentForSearchText:searchString scope:nil];
    return YES;


}

- (void) searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
    // iOS7 Hack
    if (!SYSTEM_VERSION_LESS_THAN(@"7.0")) {
        controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f);
    }

}

Thank you for advance.

Possbile duplicate: UISearchBar's Cancel and Clear Buttons Not Working in iOS 7

EDIT:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.searchDisplayController.searchBar.hidden = YES;
    self.tempImageView.hidden = NO;
    [searchBar resignFirstResponder];
}

SOLUTION:

With this function I resolved the problem:

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

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

Hope it helps!

IOS 7 default search bar will be transparent cancel button

- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
    [aSearchBar resignFirstResponder];
    isSearching = NO;
    aSearchBar.text = @"";

    [diaryTableView reloadData];

}

With this function I resolved the problem:

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

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

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM