我在UISearchController
中使用了简单的UITableViewController
。当我在第一个控制器中使用搜索栏时出现问题,该搜索栏在下一个或后面隐藏时显示UI故障。
以下是用于设置导航栏的代码。
self.extendedLayoutIncludesOpaqueBars = true
let navigationBar = navigationController.navigationBar
let navigationBarTitleTextAttritbutes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationBar.titleTextAttributes = navigationBarTitleTextAttritbutes
if #available(iOS 11.0, *) {
navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationBar.prefersLargeTitles = false
}
navigationBar.isTranslucent = false
navigationBar.tintColor = UIColor.white
navigationBar.barTintColor = UIColor.purple
并且用于设置如下所示的搜索控制器
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.hidesBottomBarWhenPushed = true
self.searchController.obscuresBackgroundDuringPresentation = false
let searchBarObj = self.searchController.searchBar
searchBarObj.delegate = self as? UISearchBarDelegate
searchBarObj.placeholder = "Search here"
searchBarObj.isTranslucent = false
searchBarObj.tintColor = .white
if #available(iOS 11.0, *) {
searchBarObj.tintColor = .white
searchBarObj.barTintColor = .white
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
} else {
let tintColor = UIColor.purple
searchBarObj.barTintColor = tintColor
searchBarObj.layer.borderColor = tintColor.cgColor
searchBarObj.layer.borderWidth = 1
if let table = tableview {
table.tableHeaderView = searchController.searchBar
}
}