问题
I added SearchBar
to my TableView
in ViewDidLoad()
doing it:
self.searchBar = UISearchController(searchResultsController: nil)
self.searchBar.searchResultsUpdater = self
self.searchBar.dimsBackgroundDuringPresentation = false
self.searchBar.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.searchBar.searchBar
self.tableView.reloadData()
everything works fine, but when I tap on this SearchBar
it disappears. It means, I can still typing, and I can see the results but, don't see SearchBar
. I implemented UISearchBarDelegate
and I have been trying to add
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
self.navigationController?.navigationBarHidden = false
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
self.navigationController?.navigationBarHidden = true
}
but it still doesn't work. Do you have any idea, why this Search Bar
disappears?
solution of this problem is (like @sandy sad) write this line of code in viewDidLoad()
self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
but now I have a new problem it's mean When I select row in TableView
and display new VievController
, SearchBar
doesn't disappear and I see it in new view. Why?
回答1:
You need to set extendedLayoutIncludesOpaqueBars to true in viewDidLoad().
self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
回答2:
Actually the search Bar
is not hiding it is just adjusting it width
and height
according to the text.
Remove this line from your code and it will work fine.
self.searchBar.searchBar.sizeToFit()
来源:https://stackoverflow.com/questions/34633176/search-bar-disappeared-from-view-while-typing