你好,我在尝试隐藏带有搜索栏的工具栏时遇到了问题。
我有一个模态视图,其中包含一个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];
}