7

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 guidance provided in http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view was followed. I recently downloaded the latest xCode (Version 5.0 (5A1413)) with iOS 7 support and have been testing the app in question on different targets.

When running this app on an iOS 6 target (emulator or real device), it works as expected meaning that pressing the cancel button removes the search bar and pressing the clear button (little gray x) clears all of the search criteria already typed in by the user. But when the project is run on an iOS 7 target both the clear and cancel button do not work.

The searchBarCancelButtonClicked method is implemented in this project, and I have verified that it is not called when the target is running iOS 7.

- (void)searchBarCancelButtonClicked:(UISearchBar *)SearchBar
{
    NSLog(@"searchBarCancelButtonClicked called");

    self.searchBar.text = nil;

    …

    // Hide Search bar when cancelled
    [self hideSeachBar];

    [self.searchBar resignFirstResponder];

    …
    }

My table view controller is setup to be the UISearchDisplayDelegate and UISearchBarDelegate. And it appears that this is still working as searchBar:textDidChange: is called on either a iOS 6 or 7 target.

@interface ItemViewController () <UISearchDisplayDelegate, UISearchBarDelegate>
…
@end

I cannot see any other posts related to this or any iOS 7 change material (like https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW1) that mentions any recoding that needs to be done to support iOS7.

Any thoughts on this? Thanks

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Mike
  • 777
  • 3
  • 7
  • 14
  • I have not had a chance to review the answers, but I also just noticed these posts for the same issue: https://devforums.apple.com/message/880210#880210 and https://devforums.apple.com/message/876407#876407. – Mike Oct 05 '13 at 12:07
  • That code work for me for iOS7: http://stackoverflow.com/a/22287788/771689 – William Wong Garay Mar 09 '14 at 20:34

6 Answers6

3

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];
}
Sahil Mahajan
  • 3,922
  • 2
  • 29
  • 43
Prabakaran
  • 620
  • 1
  • 7
  • 8
  • Curious how your self.archiveListTblView is set and used. – Mike Oct 05 '13 at 13:22
  • Hello Mike, In my point of view : iOS7 is automatically makes the layout for its nativity windows. That is the reason for the problem which we have faced. And one more thing, I have tried with "self.view.frame" instead of "self.archiveListTblView.frame". Even this code too working fine for me. So, the problem was we didn't set the frame for tableview. :-P – Prabakaran Oct 07 '13 at 11:39
1

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.

nahung89
  • 7,745
  • 3
  • 38
  • 40
1

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.

André Rodrigues
  • 9,313
  • 4
  • 23
  • 25
1

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

Sanandrea
  • 2,112
  • 1
  • 27
  • 45
0

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.

Kiddo
  • 5,052
  • 6
  • 47
  • 69
0

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.

Ahn Sejong
  • 11
  • 2