我有搜索栏,当点击图片时,它会在顶部导航创建搜索栏,替换为标题。然后当在搜索栏外点击时,标题不会再次显示,它仍然带有搜索栏(文本字段),如果在搜索栏外点击或点击如何再次显示标题?
这是在顶部导航创建搜索栏的代码,替换为标题。
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"
}
但是我要如何再次在顶部导航中显示标题?
最佳答案
你需要设置为nil
self.navigationItem.titleView = nil
参见:https://developer.apple.com/reference/uikit/uinavigationitem/1624935-titleview
关于swift - 如何将 self.navigationItem.titleView 快速替换为搜索栏后的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41864458/