使用UISearchBar时隐藏UIToolBar

3

你好,我在尝试隐藏带有搜索栏的工具栏时遇到了问题。

我有一个模态视图,其中包含一个UIToolBar、一个UISearchBar(和它的控制器)以及一个UITableView。 我使用UIToolbar是因为这个视图实际上是作为模态视图显示的。我想在UINavigationController的上下文中尝试做这件事可能会更容易一些。

当搜索时,我想隐藏工具栏。为此,我使用通知来在键盘出现时改变组件的框架。 但是我有一个问题,我的搜索栏下面有一个突出显示的空格。你可以看到一个截图:

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

我使用NSNotifcationCenter来接收关于键盘将要隐藏/显示的通知:

- (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];
}

以下是我的回调函数:
- (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个回答

0

您可以在适当的时候使用以下方法来隐藏它:

toolbar.hidden = YES;

如果我使用这个,我只会在视图顶部得到一个空白区域。我需要将搜索栏移动到视图的顶部。我知道使用navigationController很容易实现这一点(使用-(void)sethidden:YES animated:YES),但我正在尝试使用UIToolBar来完成这个任务。 - KIDdAe
你可以考虑使用动画来调整视图大小以填补空白。 - user843337
是的,这就是我想做的。但是你可以在截图中看到,我有一个被“高亮”的空格问题。 - KIDdAe
我看不到截图 - 它说我没有权限。但是话说回来,我认为你需要将工具栏移出视图。如果你还记得,左上角是位置(0,0)。因此,如果你执行一个动画,将工具栏的y坐标设置为-(工具栏的高度)时,它就会离开屏幕,当你想要它出现在屏幕上时设置为(0,0)。至于tableview,当工具栏不在屏幕上时,你应该能够将其定位于(0,0),而当工具栏在屏幕上时,将其定位于(工具栏高度,0)。 - user843337
好的,非常抱歉,我已经使用正确的公共链接编辑了我的先前问题!我认为您将通过这张图片更好地理解我的问题!实际上,在我的回调函数中,我正在按照您所说的做(也许有错误?但我真的找不到它...) - KIDdAe
啊哈,我现在明白问题所在了。就我所知,你的代码看起来没什么问题。老实说,我有点困惑这个问题出在哪里。唯一能建议的是再次检查所有的IBOutlets是否正确连接,尝试清理并重新构建应用程序,看看是否有任何不同。很抱歉我不能提供更多帮助! - user843337

-1

为什么不直接使用呢?

toolbar.hidden = TRUE;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接