繁体   English   中英

UISearchController 在 iOS 13 上无法正常工作

[英]UISearchController not working properly on iOS 13

当设备在 iOS 13 上旋转时,我在 UISearchController 上遇到问题。

我有一个 tableView,这个 tableView 有一个 searchBar 作为 header。 在 iOS 13 版本发布之前,这一直正常工作,现在旋转设备上出现了不需要的行为。

如果设备处于纵向并且 searchBar 处于焦点位置,那么我将设备旋转到横向,则 searchBar 不在正确的 position 处; 正如您在下图中看到的那样,搜索栏必须进入黄色视图,但它有点低于它

在此处输入图像描述

但是当按下取消按钮时,搜索栏会转到正确的 position。

在此处输入图像描述

我已经对其进行了调试,并且视图似乎是在正确的位置创建的,包括帧大小和原点。

当焦点在搜索栏上并且方向为横向时会发生类似的行为,然后将其更改为纵向。

我添加搜索栏的代码:

func setupSearchBar() {
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.delegate = self
    searchController.definesPresentationContext = true
    searchController.searchBar.returnKeyType = UIReturnKeyType.done
    searchController.searchBar.placeholder = "search here ..."
    searchController.searchBar.tintColor = UIColor.red
    searchController.searchBar.delegate = self

    let yellowView: UIView = UIView.init(frame: searchController.searchBar.frame)
    yellowView.backgroundColor = UIColor.yellow
    yellowView.addSubview(searchController.searchBar)

    tableView.tableHeaderView = yellowView
    tableView.reloadData()
}

我通过创建自定义 UISearchController 解决了这个问题。

class CustomSearchController: UISearchController {

    var _searchBar: UISearchBar = UISearchBar.init()
    override var searchBar: UISearchBar {
        get { 
            return _searchBar 
        }

        set { 
            _searchBar = newValue 
        }
    }

}

我没有使用 UISearchController 提供的默认 UISearchBar,而是通过添加到 tableView 的新 UISearchBar 覆盖它。

在此处输入图像描述

func setupSearchBar() {
    searchController.searchBar = searchBar
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.delegate = self
    searchController.definesPresentationContext = true
    searchController.searchBar.returnKeyType = UIReturnKeyType.done
    searchController.searchBar.placeholder = "search here ..."
    searchController.searchBar.tintColor = UIColor.red
    searchController.searchBar.delegate = self

    tableView.tableHeaderView = searchController.searchBar
}

此解决方案适用于 iOS 9 或更高版本,并保持应用程序以前的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM