ios - iOS13 中的搜索 Controller

标签 ios swift uisearchcontroller ios13

我正在操作中的 navigationItem 中添加 searchController。在 iOS12 中可以正常显示,但在 iOS 13 中则无法显示。在调试 View 层次结构时,显示搜索栏已添加,但其高度已设置为零。无法找到正确的解决方案。

代码:

func setupSearchController() {

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self as UISearchResultsUpdating
    searchController.searchBar.placeholder = "Search here..."
    searchController.searchBar.delegate = self as UISearchBarDelegate
    searchController.searchBar.tintColor = .white
    searchController.searchBar.barTintColor = .white
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.showsCancelButton = true 

    if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField {

        textfield.textColor = UIColor.blue

        if let backgroundview = textfield.subviews.first {

            // Background color
            backgroundview.backgroundColor = UIColor.white

            // Rounded corner
            backgroundview.layer.cornerRadius = 10;
            backgroundview.clipsToBounds = true;
        }
    }

    if #available(iOS 11.0, *) {
        self.navigationItem.searchController = searchController
    } else {
        self.tblView.tableHeaderView = searchController.searchBar
    }
}

在调试导航器中,

enter image description here

在尺寸检查器中,

enter image description here

最佳答案

尝试设置搜索栏框架

searchController. searchBar.frame = CGRect(x: 0, y: 0, width:view.frame.size.width, height: 50)

关于ios - iOS13 中的搜索 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58178804/

相关文章:

ios - 我读过 MVC 可以通过 UIViewController 中的这个方法找出它的大小类环境

ios - 使用 OS X Finder 以外的其他工具压缩 iPhone 应用程序以进行提交

swift - 为什么我的 Swift 代码会尝试调用错误的 init 方法?

ios - TableHeaderView 中的搜索栏行为错误

ios - Swift - 关闭搜索 Controller

ios - NSOperationQueue 在完成任务之前获得完成通知

ios - 如何在swiftui中使用内部选择器中的切换以及如何基于切换来更改选择器值

ios - 将 SSL https 与 socket.io 和 swift 一起用于 iOS 应用程序

swift - 数组作为参数不适用于 Alamofire

ios - 使用 SearchController 后的 DidSelect 导致崩溃 swift