Search Bar Cancel Button don´t work in ios 7 sometimes

断了今生、忘了曾经 提交于 2019-12-18 16:47:44

问题


Cancel button in search bar don´t work in iOS 7 when search bar is initially hidden.

I follow this tutorial to create a search bar in tableview:

raywenderlich tutorial

There are a example project in this tutorial, is better use this project than my explanation :)

In iOS 5 and 6 works fine. I have reviewed all delegates.

There are two possibilities. The first is to press the button when the bar is hidden, the second is to press the button when the bar is displayed (moving the table down with a gesture you can see the search bar)

If search bar is hidden initially cancel button don´t work, it don't call calcel delegate method:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

Sorry I can not explain it better.

Thanks


回答1:


I googled all over internet and couldn't find a solution. so i changed the UItableview behaviour.

instead of [searchBar becomeFirstResponder]; I scroll down the tableview.

- (IBAction)goToSearch:(id)sender {

 scroll down to show the table.
//    CGRect newBounds = self.tableView.bounds;
//    newBounds.origin.y =0;
//    
//    self.tableView.bounds = newBounds;
//[searchBar becomeFirstResponder];

    CGPoint contentOffset=self.tableView.contentOffset;
    contentOffset.y=0;
    [self.tableView setContentOffset:contentOffset animated:YES];


}

in my ViewDidload:

//        CGRect newBounds = self.tableView.bounds;
//        newBounds.origin.y = newBounds.origin.y + searchBar.bounds.size.height;
        // self.tableView.bounds = newBounds;

        CGPoint contentOffset=self.tableView.contentOffset;
        contentOffset.y=self.tableView.bounds.origin.y + searchBar.bounds.size.height;
        self.tableView.contentOffset=contentOffset;

If found for some reasons, in iOS 7 , change table view bounds cause search bar to disappear. Hope that helps.




回答2:


That code work for me on iOS7:

- (IBAction)goToSearch:(id)sender {
  [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
  [candySearchBar becomeFirstResponder];
}



回答3:


put this code in your project it will work i have tested and it is working correctly

 -(void)searchBarSearchButtonClicked:(UISearchBar *)searchbar
{
[searchbar resignFirstResponder];

for (UIView *possibleButton in searchbar.subviews)
{
    if ([possibleButton isKindOfClass:[UIButton class]])
    {
        UIButton *cancelButton = (UIButton*)possibleButton;
        cancelButton.enabled = YES;
        break;
    }
}

}



回答4:


This problem seems to come from the new behaviour of the translucent property in a navigation bar.

Since iOS 7 navigation bars are translucent by default. And it looks like it's overlapping the search bar when you display it after pressing a button.

Try to set in your controller:

float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (osVersion >= 7.0)
{
    self.navigationController.navigationBar.translucent = NO;
}

This should quickly solve the problem.

But I think for a better fix you should see the iOS 7 transition guide where they explain how to handle translucent navigation bars.

Hope that helps.




回答5:


I assume you have set _searchBar.delegate = self and implemented UISearchBarDelegate in your class.

This is how you do it:

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

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{                     // called when cancel button pressed
    searchBar.text = nil;

    //hide cancel button
    [_searchBar setShowsCancelButton:NO animated:YES];

    [searchBar resignFirstResponder];
}


来源:https://stackoverflow.com/questions/19245779/search-bar-cancel-button-don%c2%b4t-work-in-ios-7-sometimes

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