Hide UIToolBar when using an UISearchBar

562 views Asked by At

Hello I'm currently encountering a problem when I try to hide a toolBar With a searchBar.

I have a modal view with an UIToolBar, an UISearchBar (and its controller) and an UITableView. I am using a UIToolbar because this view is actually displayed as a modal view. I guess that what I am trying to do would a bit easier in the context of a UINavigationController.

When searching, I want to hide the toolBar. For this I use notifications to change the frame of my components when the keyboard appears. But I have a problem, there is an highlighted space under my searchBar. You can see a screenshot :

http://dl.dropbox.com/u/39339665/Capture%20d%E2%80%99%C3%A9cran%202011-10-19%20%C3%A0%2016.21.43.png

Are is were I use NSNotifcationCenter to get notified when the keyboard will be hidden/shown:

- (void)viewDidLoad {
[super viewDidLoad];
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

Here are my callback:

- (void)keyboardWillHide:(NSNotification *)notification
{
  [UIView beginAnimations:nil context:nil];
  CGRect toolbarFrame = self.toolBar.frame;
  toolbarFrame.origin.y += toolbarFrame.size.height;

  CGRect tableViewFrame = self.theTableView.frame;
  tableViewFrame.origin.y += toolbarFrame.size.height;
  tableViewFrame.size.height -= toolbarFrame.size.height;

  self.toolBar.frame = toolbarFrame;
  self.theTableView.frame = tableViewFrame;  
  [UIView commitAnimations];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
  [UIView beginAnimations:nil context:nil];
  CGRect toolbarFrame = self.toolBar.frame;
  toolbarFrame.origin.y -= toolbarFrame.size.height;

  CGRect tableViewFrame = self.theTableView.frame;
  tableViewFrame.origin.y -= toolbarFrame.size.height;
  tableViewFrame.size.height += toolbarFrame.size.height;

  self.toolBar.frame = toolbarFrame;
  self.theTableView.frame = tableViewFrame;
  [UIView commitAnimations];
}
2

There are 2 answers

0
ader On

why not just use

toolbar.hidden = TRUE;
6
AudioBubble On

You can hide it at the appropriate time using:

toolbar.hidden = YES;