【问题标题】:UISearchBar going to the view's top when I click on it当我点击它时,UISearchBar 会转到视图的顶部
【发布时间】:2013-12-27 23:32:23
【问题描述】:

我有一个没有到达整个视图的 UITableView。大约,UITableView 比视图小两倍。

然后,我将 “搜索栏和搜索显示控制器”对象库 中添加到 UITableView。因此,为了形象化,searchBar 出现在屏幕的中心。

这是我的看法:

我的问题:当我点击搜索字段进行研究时,searchBar 会转到视图顶部,而不是导航栏!而且过渡非常糟糕......我怎样才能约束 searchBar 留在它的初始位置?

【问题讨论】:

  • 有同样的问题,我今天要问它。搜索栏旨在移动到顶部,但问题是它在桌子上留下的空白空间。我相信由于控制器的视图不仅仅是一个表格视图,那么搜索栏不能直接操纵表格视图向上移动并覆盖空白空间
  • @giorashc 是否为 self.searchDisplayController 或 self.searchDisplayController.searchResultsTableView 设置了框架。尝试为它设置框架.. 让我知道 -
  • 使用简单的 UISearchBar 代替 UISearchDisplayController
  • @the1pawan 我尝试通过修改其框架来移动表格视图,但它不起作用。就我而言,我不在乎搜索栏在顶部,但它在表格视图中留下的空白空间是我的问题
  • @AshutoshMishra 仅使用 UISearchBar 时不会出现问题。但是如果不使用 UISearchDisplayController,您将无法访问诸如 shouldReloadTableForSearchStringshouldReloadTableForSearchScope... 之类的委托方法

标签: ios iphone uisearchbar uisearchdisplaycontroller


【解决方案1】:

问题与搜索显示控制器有关。您无法控制动画,并且当您的 TableView 顶部有一个视图时,它似乎无法很好地处理它。我们遇到了同样的问题,我们修复它的最简单方法是将搜索显示控制器替换为情节提要中的简单搜索栏。

这意味着您的类现在必须实现搜索栏的委托方法才能正常工作。

要正确设置动画,只需像这样覆盖 searchBarTextDidBeginEditingsearchBarTextDidEndEditing

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.origin.y = tableViewFrame.origin.y - self.yourHeaderView.bounds.size.height;
[UIView animateWithDuration:0.25
                 animations:^{ self.tableView.frame = tableViewFrame; }];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.origin.y = tableViewFrame.origin.y + self.yourHeaderView.bounds.size.height;
[UIView animateWithDuration:0.25
                 animations:^{ self.tableView.frame = tableViewFrame; }];
}

简单地说,您只需根据高度将表格视图框架移动到视图上。动画使其更流畅、更清晰。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2014-03-06
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多