我正在将searchController添加到navigationItem on action中。在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中的,

In Size检查器,

发布于 2019-10-01 06:07:23
试着设置搜索栏的框架
searchController. searchBar.frame = CGRect(x: 0, y: 0, width:view.frame.size.width, height: 50)发布于 2019-10-01 17:55:59
您的代码对我来说很好:

也许你的导航栏已经倒塌了?如果导航项的hidesSearchBarWhenScrolling属性为true,则导航条可以折叠以隐藏搜索栏。
https://stackoverflow.com/questions/58178804
复制相似问题