iOS, swift, move UISearchBar from tableHeaderView to UINavigationItem

662 views Asked by At

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): enter image description here

When I tap on search bar(normal): enter image description here

When I tap on cancel button (not normal):

  1. I need only search bar view correction (do not worry about navigation bar)
  2. I want to deselect search bar when cancel button function execute (without self.moveSearchBarToTableHeader() (and self.moveSearchBarToNavigationBar()) - search bar deselect, but within - not) enter image description here
0

There are 0 answers