【问题标题】:Double tap UISearchBar with search delegate on iOS 7 causes UISearchBar to disappear在 iOS 7 上双击带有搜索委托的 UISearchBar 会导致 UISearchBar 消失
【发布时间】:2013-10-14 11:42:18
【问题描述】:

我们在表格标题中有一个搜索栏。当用户在 iOS 7 上快速点击它两次时,它就会消失。有人对我们做错了什么有任何建议吗?

【问题讨论】:

  • 它在导航栏下吗?我有几个视图的搜索栏并且工作正常。
  • 我也遇到了同样的问题。我尝试从代码中删除以下行。 self.edgesForExtendedLayout=UIRectEdgeNone; self.navigationController.navigationBar.translucent = NO;它解决了这个问题。但是用户界面会受到干扰。

标签: ios7 uisearchbar uisearchbardelegate uisearchbardisplaycontrol


【解决方案1】:

经过大量的尝试和错误,我发现当 searchDisplayController 结束搜索时,搜索栏消失了,所以我将搜索栏重新插入到表头,它对我有用。

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    self.searchingFetchedResultsController = nil;
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView];
    }
    return;
}

希望这会有所帮助。

【讨论】:

  • 我也遇到了同样的错误。
  • 感谢 Priya,这似乎基本上可以工作: - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { if (IS_IOS7_OR_GREATER) { [self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.表视图]; } }
  • 令人惊讶的是它确实有帮助,但解决方案有多脏,我真的不喜欢我也必须使用它。当用户再次点击已经动画的搜索视图时会出现问题,其中另一个点击是由 searchDisplayController 的空结果表接收 - 导致它立即关闭。
【解决方案2】:

(我在Troubles with UISearchBar \ UISearchDisplayViewController 上发布了同样的答案,这似乎是这个问题的重复。)

我遇到了同样的问题,并注意到searchDisplayControllerDidEndSearch 被调用了两次。第一次self.searchDisplayController.searchBar的superview是UITableView,第二次还是UIView

根据 Priya 的回答,我担心每次双击搜索栏时重新插入子视图会产生意想不到的后果或不必要的开销,并且我还担心它会在未来的 iOS 版本中中断。幸运的是,我们可以像这样利用 superview 的奇异性:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    if (self.tableView != self.searchDisplayController.searchBar.superview) {
        [self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView];
    }
}

如果我不得不猜测发生了什么,UISearchBar 在它处于活动状态时会自动创建一个临时的UIView 作为其父视图——这是执行搜索时看到的视图。当UISearchBar 被解除时,超级视图被设置回它之前的UITableView除非它被迅速解除以至于它从未正确初始化,在这种情况下它会清理不正确,UITableView 永远不会将UISearchBar 作为其子级返回。

这个解决方案仍然不理想,我认为 Apple 必须在自己的应用程序中做一些不同的事情,因为他们的搜索栏 UX 感觉更好一些。我认为在UISearchBar 准备好之前,最好不要先处理第二次点击。我尝试使用其他 UISearchBarDelegate 方法来执行此操作,但找不到合适的挂钩来覆盖当前行为。

【讨论】:

  • 我自己也遇到了这个错误。如果您检查window 是否为零怎么办?这样,您就知道它不在视图层次结构中,并且您不必对相等性检查进行硬编码。
猜你喜欢
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
相关资源
最近更新 更多