首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UISearchController在iOS 13上无法正常工作

UISearchController在iOS 13上无法正常工作
EN

Stack Overflow用户
提问于 2019-10-02 04:31:52
回答 1查看 2.1K关注 0票数 0

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

我有一个tableView,这个tableView有一个searchBar as头。在iOS 13发布之前,这是正常工作的,现在旋转设备上有一个不想要的行为。

如果设备处于纵向并且searchBar处于对焦状态,则我将设备旋转到横向searchBar不在正确的位置;正如您在下面的图像上看到的那样,搜索栏一定是在黄色视图中,但它有点低于它

但是当按下cancel按钮时,搜索栏就会移到正确的位置。

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

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

添加搜索栏的代码:

代码语言:javascript
运行
复制
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()
}
EN

回答 1

Stack Overflow用户

发布于 2019-10-03 03:05:51

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

代码语言:javascript
运行
复制
class CustomSearchController: UISearchController {

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

        set { 
            _searchBar = newValue 
        }
    }

}

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



代码语言:javascript
运行
复制
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或更高版本,并保持应用程序以前的行为。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58191878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档