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

前端 未结 6 1038
生来不讨喜
生来不讨喜 2020-12-07 02:09

I have an xCode project that contains a tableview with a “Search Bar and Search Display Controller” to allow the user to refine the list of displayed items. In general, the

相关标签:
6条回答
  • 2020-12-07 02:26

    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.

    0 讨论(0)
  • 2020-12-07 02:31

    I have had this problem too. Strangely other delegate methods of UISearchBarDelegate are being called. A workaround might be:

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
        if ([searchText length] == 0) {
            NSLog("No Text");
        }
    }
    

    It worked for me

    0 讨论(0)
  • 2020-12-07 02:34

    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. If you scroll to the top of your listView and use the search bar, it should be working correctly.

    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.

    0 讨论(0)
  • 2020-12-07 02:36

    button Cancel and button Clear won't work if you touch button Search on navigation. If you tap on Search bar to star searching, it works normally => That mean if search bar is not visible on screen before staring search, then those buttons seem like disable too.

    So that, I found a solution, but it is not totally perfect. Before you make search bar become first response, you have to scroll table view to top. Try this code.

    -(IBAction)goToSearch:(id)sender {
         // Make search bar visible on screen before make it response
        [_tableView setContentOffset:CGPointMake(0, 0) animated:NO];
    
        // Make search bar active
        [candySearchBar becomeFirstResponder];
    }
    

    The only issue occur if you do this, is your table view will scroll to top, which mean when you cancel search, you lost current cell index before.

    0 讨论(0)
  • 2020-12-07 02:37

    I have the same problem, I tried with the following code. Please try this one.

    -(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];
    }
    
    0 讨论(0)
  • 2020-12-07 02:48

    In my case, I had repositioned the searchbar to the top of the screen, and there was an invisible view which is overlapped over the searchbar.

    Thus cancel button was not touched actually.

    So, I have brought the seachbar front when [searchBarTextDidBeginEditing] method was called like below.

    -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
        lc_SearchBarYPos.constant = 20.0f; //this is code to reposition the searchbar
        [self.view bringSubviewToFront:searchBar];
    }
    

    Hope this may help.

    0 讨论(0)
提交回复
热议问题