I have tableview. In the table view, I have UISearchBar in the tableHeaderView (I defined it in UIStoryBoard). I want to move UISearchBar from tableHeaderView to UINavigationItem when I tap to search bar. And I want to return all back when I tap cancel button.
Problem: I cant to return UISearchBar to tableHeaderView.
func moveSearchBarToNavigationBar()
{
self.searchBar.removeFromSuperview()
self.tableView.tableHeaderView = nil
self.navigationItem.titleView = self.searchBar
self.navigationItem.leftBarButtonItem = nil
}
func moveSearchBarToTableHeader()
{
self.searchBar.removeFromSuperview()
self.tableView.tableHeaderView = self.searchBar
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar)
{
self.searchActive = true
self.searchBar.showsCancelButton = true
self.moveSearchBarToNavigationBar()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar)
{
self.searchActive = false
self.searchBar.showsCancelButton = false
self.searchBar.text = ""
self.searchBar.resignFirstResponder()
self.searchBar(self.searchBar, textDidChange: "")
self.moveSearchBarToTableHeader()
}
First view (normal):
When I tap on search bar(normal):
When I tap on cancel button (not normal):
- I need only search bar view correction (do not worry about navigation bar)
- I want to deselect search bar when cancel button function execute (without self.moveSearchBarToTableHeader() (and self.moveSearchBarToNavigationBar()) - search bar deselect, but within - not)