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作为SubView添加到此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.
这可能是什么问题?我已经尝试将SearchBar中的AutoLayout约束添加到其父视图中,但它似乎没有改变任何东西。是不是[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的框架设置为容器的视图边界
- Use an additional container view
- put UISearchController's view inside
- set UISearchController's view autoresizing mask to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
- set UISearchController's frame to container's view bounds
此时UISearchController.view赢了' t调整大小超出container.bounds。
At this point UISearchController.view won't resize beyond container.bounds.
这篇关于UISearchController searchBar框架仅在第一次单击时调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!