views:

4077

answers:

2

Hi,

I would like to hide the UISearchBar most of the time and only call it to appear when user wants it.

I've put a UISearchBar in Interface Builder and hide it behind a view, when user click a button, it calls the following code, which I hoped it would bring the search bar to the front and slide the keyboard to view. But it doesn't....

- (IBAction)search:(id)sender
{
   [mySearchBar performSelector:@selector(searchBarTextDidBeginEditing:)];
}

Anyone have any idea how to activate UISearchBar by code?

Thanks.

Thanks for your replies. I just post the complete code in case someone's interested

- (IBAction)search:(id)sender
{
    mySearchBar.hidden = NO;
    [mySearchBar becomeFirstResponder];
}

#pragma mark UISearchBarDelegate delegate methods

// called when keyboard search button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    mySearchBar.hidden = YES;
    [mySearchBar resignFirstResponder];
// Do something with the mySearchBar.text
}

// called when cancel button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    mySearchBar.hidden = YES; 
    [mySearchBar resignFirstResponder];
}
+1  A: 

I placed the search bar on top of the view and made it hidden. Then you just need:

mySearchBar.hidden = NO;

to display it. Depending on what you're doing you may also need to resize the underlying view (otherwise the top part of it will be cropped). And it still doesn't "slide" into view. It may be easier to use a completely new view with the search bar attached appropriately.

Stephen Darlington
+1  A: 

If you want to pop the keyboard, you'll need to call [mySearchBar becomeFirstResponder]

Ben Gottlieb
that's great hint, cheers
Dan