UISearchController searchBar 框架仅在第一次单击时调整大小 [英] UISearchController searchBar frame only resizes on first click
问题描述
我刚刚在一个普通的 UIViewController 中实现了一个 UISearchController,我遇到了一个奇怪的问题.
I have just implemented a UISearchController in a regular UIViewController, and I have a kind of weird issue.
我有一个 UIView,它完全适合我希望 UISearchBar 的大小.在第一次启动视图时,我将我的 UISearchBar 作为子视图添加到这个 UIView.但是当我启动它时, UISearchBar 不占用其父 UIView 的大小——它只占用整个屏幕的宽度.
I have a UIView that is adapted exactly to the size I want my UISearchBar to take. On first launch of the view, I add my UISearchBar as a SubView to this UIView. But when I launch it, the UISearchBar doesn't take the size of its parent UIView -- it just takes the width of the whole screen.
如您所见,它与右侧的按钮重叠.
As you can see, it overlaps with the button on the right.
但是一旦我点击搜索栏并取消它,它就会调整到我想要的确切大小.
But once I click on the search bar and cancel it, it resizes to the exact size I want.
这可能是什么问题?我尝试将 AutoLayout Constraints 从 SearchBar 添加到其父视图,但它似乎没有任何改变.[UIView addSubview:] 不处理这个吗?
What could be the issue here? I've tried adding AutoLayout Constraints from the SearchBar to its parent view but it doesn't seem to change anything. Doesn't [UIView addSubview:] handle this?
推荐答案
UISearchController 的视图行为确实很奇怪,但有一个简单的解决方法.
UISearchController's view behaviour is weird indeed, but there is a simple workaround.
- 使用额外的容器视图
- 将 UISearchController 的视图放入其中
- 将 UISearchController 的视图自动调整大小掩码设置为 UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight
- 将 UISearchController 的框架设置为容器的视图边界
此时 UISearchController.view 的大小不会超过 container.bounds.
At this point UISearchController.view won't resize beyond container.bounds.
这篇关于UISearchController searchBar 框架仅在第一次单击时调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!