3

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];
}
KIDdAe
  • 2,714
  • 2
  • 22
  • 29

2 Answers2

0

You can hide it at the appropriate time using:

toolbar.hidden = YES;
  • If I use this I'll just have a white space at the top of the view. I need to move the searchBar at the top of the view. I know that it's easy to do this with the navigationController (using -(void)sethidden:YES animated:YES), but I'm trying to do this with a UIToolBar – KIDdAe Oct 19 '11 at 16:29
  • You could always consider using an animation to resize your view to fill the gap? –  Oct 19 '11 at 16:50
  • Yes, that is what I'm trying to do. But as you can see in the screenshot, I have a problem with a space that is "highlighted" – KIDdAe Oct 19 '11 at 17:17
  • I can't see the screenshot - It says I don't have access. However that said, I think you need to move the toolbar off the view. If you recall, the top left corner is position (0,0). So if you carry out an animation to set the y coordinate of the toolbar to be -(height of toolbar) when you want it offscreen, and (0,0) when you want it onscreen. As for the tableview you should be able to position it at (0,0) when the toolbar if off-screen, and at position (toolbar height, 0) when the toolbar is onscreen. –  Oct 19 '11 at 19:08
  • Ok, I'm really sorry, I edit my previous question with the correct public link ! I think that you will have a better understanding of my problem with this picture ! In fact I'm doing exactly what you are saying in my callbacks (maybe with a mistake ? but I really don't find it ...) – KIDdAe Oct 19 '11 at 22:11
  • Ahhh I see the issue now. Your code looks fine as far as I can tell. To be completely honest I'm a bit stumped as to what the issue is. The only thing I can suggest is to double check all of your IBOutlets are linked up correctly, try cleaning and rebuilding the app and see it makes any difference. Sorry I can't be of more help! –  Oct 20 '11 at 10:52
-1

why not just use

toolbar.hidden = TRUE;
ader
  • 5,403
  • 1
  • 21
  • 26