iphone - 使用 UISearchBar 时隐藏 UIToolBar

标签 iphone uitableview uisearchbar uitoolbar modal-view

您好,当我尝试使用搜索栏隐藏工具栏时,我目前遇到了问题。

我有一个带有 UIToolBar、UISearchBar(及其 Controller )和 UITableView 的模态视图。 我使用 UIToolbar 因为该 View 实际上显示为模态视图。我想我想要做的事情在 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];
}

最佳答案

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

toolbar.hidden = YES;

关于iphone - 使用 UISearchBar 时隐藏 UIToolBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7823452/

相关文章:

ios - observables 和 UItableview 可见单元格

ios - 如何防止搜索栏在滚动时消失? iOS swift

iphone - 在 xib 中将 UISearchBar 添加为 tableHeaderView 不起作用

ios - 如何在 UISearchController 中自定义 searchBar?

iphone - 使用AssetsLibrary Framework加载图像太慢

iphone - 是否有适用于 iPhone 应用程序的非采样时间分析工具?

iphone - 切换到另一个选项卡时使标题 View 不变

swift - 在 ViewController 中为 UITableView 编辑和添加按钮

ios - 带有 insertRowAtIndexPath 的动态 UITableViewCell

iphone - NSString *string = @"someString"vs NSString *string = [[NSString alloc] initWithFormat @"%@", string]