1

I have a UISearchBar on a bottom toolbar. The problem is the keyboard covers it up once you click on it. I was trying to think of ways around this. I tried adding it to the top, but since I'm using a UINavigationController, I'm getting some unexpected behavior. (Like the UISearchBar in the customView for the rightBarButtonItem lengthens once the keyboard is on screen and doesn't return to its original size, and the entire NavigationBar goes off the screen when the keyboard is pressed so you cannot even see what you are typing in that situation either.)

So as a workaround, I was thinking I could have the systemIcon for search in the lower right corner of my toolbar, and then when the keyboard pops up, the searchbar could be part of the keyboard, and then you could see what you type. Does anyone know how I would do that? Thanks.

Crystal
  • 28,460
  • 62
  • 219
  • 393

2 Answers2

2

From Apple's HIG for IOS:

Display a search bar above a list or the index in a list. Users expect to find a search bar in this position, because they're accustomed to the search bar in Contacts and other applications. Putting the search bar in this location means that it stays out of users' way when they're scrolling through the list or using the index, but is conveniently available when it's needed.

Maybe showing a search bar at the bottom of the screen isn't meant to be ; )

Placing the UISearchbar at the top in a custom ViewController should work. Maybe share some code?

Webdevotion
  • 1,223
  • 13
  • 24
1

Beryllium is on the right track. here is some code that will help you get there.

  1. Move the search bar off the screen so it cant be seen until the button is pushed
  2. Register for keyboardWillShow notifications
  3. when button is pushed, make the searchbar "becomeFirstResponder"
  4. move the search bar from the bottom (out of view) to above the keyboard (in view)

//

//in viewDidLoad register for notifications
 [self registerForKeyboardNotifications];

//in viewdidload hide the search bar
CGRect rect = searchBar.frame;
rect.origin.y = 480;
searchBar.frame = rect;

//own function register for keyboardwillshow
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


//when button is pushed move the searchbar into view. you may have to play with the pixels a bit to make sure it lands on top, and flush

- (void)keyboardWillShow:(NSNotification*)aNotification
{

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGRect rect = searchBar.frame;
rect.origin.y = 180; //adjust the 180 to get it to land where you want
searchBar.frame = rect;
[UIView commitAnimations];
}


//own function hide the search bar when the keyboard is dismissed
- (void)keyboardWillHide:(NSNotification*)aNotification
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGRect rect = searchBar.frame;
rect.origin.y = 480;
searchBar.frame = rect;
[UIView commitAnimations];

}
Louie
  • 5,920
  • 5
  • 31
  • 45
  • How would you apply this to an application that supports turning the phone sideways? and with phones having different resolutions it won't have the same effect. – Lloyd Powell Jul 17 '12 at 13:31