我在UIViewController
中嵌入了UINavigationController
,其中UICollectionView
填满了屏幕。在UINavigationBar
我有一个按钮,在选中时会在UISearchBar
下显示UINavigationBar
。我这一切都很好。问题是,在UISearchBar
上选择取消按钮时,我无法让搜索栏解除。这是我的代码:
var searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search for Title"
self.navigationItem.searchController = searchController
}
@IBAction func searchSelected(_ sender: UIBarButtonItem) {
searchController.isActive = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("cancel clicked")
searchController.isActive = false
}
答案 0 :(得分:0)
使用:
navigationItem.searchController = nil