gpt4 book ai didi

ios - UISearchController searchBar 框架仅在第一次点击时调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:24:57 24 4
gpt4 key购买 nike

我刚刚在常规 UIViewController 中实现了 UISearchController,但我遇到了一个奇怪的问题。

我有一个 UIView,它完全适合我希望 UISearchBar 采用的大小。首次启动 View 时,我将 UISearchBar 作为 subview 添加到此 UIView。但是当我启动它时,UISearchBar 不采用其父 UIView 的大小——它只采用整个屏幕的宽度。

enter image description here

如您所见,它与右侧的按钮重叠。

但是一旦我点击搜索栏并取消它,它就会调整到我想要的确切大小。

enter image description here

这可能是什么问题?我尝试将 SearchBar 中的 AutoLayout Constraints 添加到其父 View ,但它似乎没有任何改变。 [UIView addSubview:] 不处理这个吗?

最佳答案

UISearchController 的 View 行为确实很奇怪,但有一个简单的解决方法。

  • 使用额外的容器 View
  • 将 UISearchController 的 View 放在里面
  • 将 UISearchController 的 View 自动调整掩码设置为 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
  • 将 UISearchController 的框架设置为容器的 View 边界

此时 UISearchController.view 的大小不会超出 container.bounds。

关于ios - UISearchController searchBar 框架仅在第一次点击时调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43278343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com