Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sliding UISearchBar

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];
}
like image 850
Dan Avatar asked Oct 21 '08 11:10

Dan


2 Answers

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

like image 191
Ben Gottlieb Avatar answered Oct 20 '22 19:10

Ben Gottlieb


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.

like image 40
Stephen Darlington Avatar answered Oct 20 '22 19:10

Stephen Darlington