【问题标题】:Hide UIToolBar when using an UISearchBar使用 UISearchBar 时隐藏 UIToolBar
【发布时间】:2023-03-21 06:14:01
【问题描述】:

您好,我目前在尝试使用 searchBar 隐藏工具栏时遇到问题。

我有一个带有 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];
}

【问题讨论】:

    标签: iphone uitableview uisearchbar uitoolbar modal-view


    【解决方案1】:

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

    toolbar.hidden = YES;
    

    【讨论】:

    • 如果我使用它,我只会在视图顶部有一个空白区域。我需要移动视图顶部的 searchBar。我知道使用 navigationController 很容易做到这一点(使用 -(void)sethidden:YES animated:YES),但我正在尝试使用 UIToolBar
    • 您总是可以考虑使用动画来调整视图大小以填补空白?
    • 是的,这就是我想要做的。但正如您在屏幕截图中看到的那样,我有一个“突出显示”的空间问题
    • 我看不到屏幕截图 - 它说我没有访问权限。尽管如此,我认为您需要将工具栏移出视图。如果你还记得,左上角是位置 (0,0)。因此,如果您执行动画以将工具栏的 y 坐标设置为 -(工具栏的高度),当您希望它离开屏幕时,以及当您希望它在屏幕上时,设置为 (0,0)。至于tableview,如果工具栏不在屏幕上,您应该能够将其定位在(0,0),而当工具栏在屏幕上时,您应该能够将其定位在(工具栏高度,0)。
    • 好的,真的很抱歉,我用正确的公共链接编辑了我之前的问题!我想你会更好地理解我这张照片的问题!事实上,我正在做你在我的回调中所说的(可能有一个错误?但我真的没有找到它......)
    【解决方案2】:

    为什么不直接使用

    toolbar.hidden = TRUE;
    

    【讨论】:

      猜你喜欢
      • 2011-02-27
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多