为什么这个UISearchBar会调整大小? [英] Why is this UISearchBar getting resized?

查看:426
本文介绍了为什么这个UISearchBar会调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下图所示,当我点击搜索字段时,我的 UISearchBar 会调整大小。它可以很好地覆盖导航栏,然后弹出......它会收缩。

As shown below, my UISearchBar is getting resized when I tap on the search field. It animates nicely to cover the navigation bar, and then pop... it shrinks.

UISearchBar 在香草 UIView 设置为 tableHeaderView 。我正在使用 UIView (而不是将 UISearchBar 设置为标题)因为我想添加额外的标题中的视图。

The UISearchBar is inside a vanilla UIView set as the tableHeaderView. I'm using a UIView (as opposed to setting the UISearchBar as the header) because I would like to put additional views in the header.

视图在XIB文件中定义, UISearchBar 锚定到其所有边界。这些限制似乎并不重要;如果我删除它们,会发生同样的问题。

The view is defined in a XIB file and the UISearchBar is anchored to all of its borders. The constraints don't seem to matter; if I remove them, the same problem happens.

检查Reveal中的视图层次结构告诉我 UIView 具有正确的大小, UISearchBar 在发生这种情况时宽度为1(?!)

Examining the view hierarchy in Reveal tells me that the UIView has the right size and the UISearchBar has width 1 (?!) when this happens.

作为实验,我将 UISearchBar 子类化,并使 intrinsicContentSize 返回 {320,44} 。有了这个, UISearchBar 显示正确,但是当我按下取消时,我得到以下异常:

As an experiment, I subclassed the UISearchBar and made intrinsicContentSize return {320,44}. With this the UISearchBar shows properly, but when I press Cancel I get the following exception:

 *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2903.23/UIView.m:8540
 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'



解决方法



如果我按代码创建所有内容,它就可以正常工作。

The workaround

If I create everything by code, it just works.

_headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
searchBar.delegate = self;
[_headerView addSubview:searchBar];

_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
_searchDisplayController.searchResultsDelegate = self;
_searchDisplayController.searchResultsDataSource = self;
_searchDisplayController.delegate = self;

_tableView.tableViewHeader = _headerView;

这里发生了什么?我显然不会再使用笔尖与 UISearchBar 一起工作,但我想了解到底发生了什么。

What is going on here? I clearly won't use nibs to work with UISearchBar anymore, but I would like to understand what the heck happened anyway.

推荐答案

区别在于搜索栏的 translatesAutoresizingMaskIntoConstraints 的值。对于基于XIB的视图,它默认为 NO ,对于基于代码的视图,默认为 YES 。显然,搜索控制器假定值为YES,并且在移植搜索栏时不会设置任何显式约束。

The difference is in the search bar's value of translatesAutoresizingMaskIntoConstraints. It defaults to NO for the XIB-based view and YES for the code-based view. Apparently, the search controller assumes a value of YES and doesn't set up any explicit constraints when it transplants the search bar.

将值设置为应该修复它(在当地验证)。

Setting the value to YES in code should fix it (verified locally).

更新

如评论中所述,即使使用 translatesAutoresizingMaskIntoConstraints = YES ,控制器也不会在取消时将标题视图恢复为原始状态。但似乎确实存在一种解决方法。您可以在标题视图中创建一个插座,并在 searchDisplayControllerDidEndSearch 中自行恢复。我做了一个粗略的概念验证(并更新了我的示例项目):

As noted in the comments, even with translatesAutoresizingMaskIntoConstraints = YES, the controller does not restore the header view to it's original state on cancel. But there does seem to be a workaround. You can create an outlet to your header view and restore it yourself in searchDisplayControllerDidEndSearch. I did a crude proof-of-concept (and updated my sample project):

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    self.tableView.tableHeaderView = self.headerView;
    [self.searchBar removeFromSuperview];
    self.searchBar.frame = CGRectMake(0, 20, 320, 44);
    [self.headerView addSubview:self.searchBar];
}

这篇关于为什么这个UISearchBar会调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆