swift - UISearchController SearchBar 在旋转期间处于事件状态时未对齐

标签 swift orientation uisearchbar uisearchcontroller

我已经为 UISearchController 的搜索栏苦苦挣扎了一段时间。我需要在 TableView 上实现搜索功能,但与传统方法不同的是,我没有将搜索栏放在表头 View 上。相反,我创建了一个 UIView 并将搜索栏添加为它的 subview 。 UIView 充当搜索栏的容器,其约束在具有自动布局的 Storyboard上正确设置。

这是我的代码。请注意,我以编程方式执行此操作,因为从 iOS 8 开始,UISearchDisplayController 和 UISearchBar 已被弃用,取而代之的是 UISearchController,并且尚未出现在 UIKit 中。

searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.autoresizingMask = .FlexibleRightMargin
searchController.searchBar.delegate = self
definesPresentationContext = true
self.searchContainerView.addSubview(searchController.searchBar)

但是,我确实注意到搜索栏在旋转过程中出现了一种奇怪的行为。当它在纵向上处于事件状态时,我将模拟器旋转到横向,然后按取消,搜索栏返回到纵向宽度。

Effects of activating search in Portrait and then Cancelling in Landscape

反之亦然。

Effects of activating search in Landscape and then Cancelling in Portrait

我将不胜感激任何想法或一些关于解决此问题的正确方向的提示,因为我至少已经这样做了好几天了。非常感谢

最佳答案

经过这么多天的努力之后:

肖像到风景

  1. 使 SearchBar/SearchController 在 Portrait 中处于事件状态
  2. 旋转成横向
  3. 按取消键,SearchBar 将返回纵向宽度

从风景到肖像

  1. 使 SearchBar/SearchController 在 Landscape 中处于事件状态
  2. 旋转成纵向
  3. 按取消键,SearchBar 将返回横向宽度

我终于自己解决了。似乎当用户在 SearchBar 上按下取消时,ViewController 将调用 viewDidLayoutSubviews 因此我尝试通过在 viewDidLayoutSubviews 中调用此函数来重置宽度:

func setupSearchBarSize(){
     self.searchController.searchBar.frame.size.width = self.view.frame.size.width
}

但这并没有我想象的那么好。所以这就是我认为会发生的事情。当用户激活 SearchController/SearchBar 时,SearchController 在调整自身大小之前保存当前帧。然后,当用户按下“取消”或关闭它时,它将使用保存的框架并调整到该框架大小。

因此,为了在我按下取消时强制重新对齐其宽度,我必须在我的 VC 中实现 UISearchControllerDelegate 并检测 SearchController 何时被关闭,然后再次调用 setupSearchBarSize()

下面是解决这个问题的相关代码:

class HomeMainViewController : UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
    @IBOutlet weak var searchContainerView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.autoresizingMask = .FlexibleRightMargin
        searchController.searchBar.delegate = self
        searchController.delegate = self
        definesPresentationContext = true
        self.searchContainerView.addSubview(searchController.searchBar)
        setupSearchBarSize()
}

func setupSearchBarSize(){
        self.searchController.searchBar.frame.size.width = self.view.frame.size.width
}

func didDismissSearchController(searchController: UISearchController) {
        setupSearchBarSize()
}

override func viewDidLayoutSubviews() {
        setupSearchBarSize()
 }
}

关于swift - UISearchController SearchBar 在旋转期间处于事件状态时未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36430104/

相关文章:

iphone - UISearchBar 和 UINavigationItem

ios - 图像不会发送到我的 Alamofire .post 请求中的后端 - Swift 3

ios - 如何将变量放入文本字​​段以及如何将所有元素转换为字符串而不仅仅是一次 1?

swift - 将 NSWindow 放置在屏幕顶部,没有 1 像素边距。

swift - 如何快速停止 UIView 动画

nullpointerexception - 方向上的动态时间显示崩溃

ios - UISearchBar 背景颜色不适用于最小的 searchBarStyle

swift - 通过启动 App 进行 Coremotion 和 iPhone 定位

iphone - 无法在 iOS 6 中旋转界面方向问题

ios - 当搜索栏具有焦点时,UISearchController 不会尊重隐藏的状态栏