我在动议的NavigationItem中添加了searchController。在iOS12中,它可以正常显示,但在iOS 13中,则没有显示。在调试视图层次结构上,它显示已添加搜索栏,但其高度已设置为零。无法找到正确的解决方案。

代码:

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
    }
}


在Debug Navigator中,

ios - iOS13中的SearchController-LMLPHP

在尺寸检查器中,

ios - iOS13中的SearchController-LMLPHP

最佳答案

尝试设置搜索栏的框架

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

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

10-13 04:12