我有搜索栏,点击图片时,会在顶部导航处创建搜索栏,替换为标题。然后当点击搜索栏外时,标题不再显示,它仍然带有搜索栏(文本字段),如果点击或单击搜索栏外部如何再次显示标题?
这是在顶部导航中创建搜索栏的代码,替换为标题。
func createSearchBar(){
searchBar.sizeToFit()
searchBar.showsCancelButton = false
searchBar.placeholder = "Enter key"
searchBar.delegate = self
self.navigationItem.titleView = searchBar
}
这是在搜索栏外点击时的代码
extension ViewCon {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewCon.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
self.searchBar.resignFirstResponder()
self.navigationController?.navigationBar.topItem?.title="Title List"
}
}
但是我想如何再次在顶部导航上显示标题?
您需要
将 to 设置为 nil
self.navigationItem.titleView = nil
请参阅: https://developer.apple.com/reference/uikit/uinavigationitem/1624935-titleview