views:

45

answers:

1

I have a tableView with a UISearchBar. Before upgrading to iOS 4, when a user selected a cell and the new viewController was pushed, the keyboard slid out of view to the left with the table.

Now, it just stays put on the screen on top of the new view. If I call [mySearchBar resignFirstResponder]; I can get rid of the keyboard, but the animation is awkward because it slides off the bottom of the screen while the new view is being pushed.

Does anyone know why the behavior would be different in iOS 4? How can I get the keyboard to be tied to the tableView so that it slides off the screen when the new view is pushed?

Thanks!

EDIT: Here's some additional information. I have the uisearchbar behind a navigationbar. It slides out when the user presses a button. I add the uisearchbar like so:

[self.navigationController.view insertSubview:self.mySearchBar belowSubview:self.navigationController.navigationBar];

I guess because the navigation bar doesn't actually get pushed to the next view (although the search bar does), the keyboard stays in place.

I tried creating a uiview behind the tableview and adding the searchbar to it, but I couldn't get it to work because of the UINavigationController and my code structure.

+1  A: 

Personally, I would recommend implementing a SearchBar which uses UISearchDisplayController. I think this implements a standard template used by xCode, and handles all the responders really smooth. Here's an example of a method you could try copying into your code and executing using

[self addSearchBar:@"enter search keywords here" hasScope:NO]

Recommended Code:

- (void) addSearchBar:(NSString*)placeholder hasScope:(BOOL)aScope {


    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    [searchBar sizeToFit];

    //searchBar.delegate = self;
    searchBar.placeholder = placeholder;
    self.tableView.tableHeaderView = searchBar;

    if (aScope==YES){
        searchBar.showsScopeBar = YES;
        searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Flags",@"Listeners",@"Stations",nil];     
    }




    UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    // The above assigns self.searchDisplayController, but without retaining.
    // Force the read-only property to be set and retained. 
    [self performSelector:@selector(setSearchDisplayController:) withObject:searchDC];

    //searchDC.delegate = self;
    //searchDC.searchResultsDataSource = self;
    //searchDC.searchResultsDelegate = self;

    [searchBar release];
    [searchDC release];

}
dpigera