title: How to show and hide the header in section of tableView by navigation bar button tapped?
body: <p>Developing an iOS application with Xcode ver 9.3, Swift.</p>
  
  <p>Could you tell me how to show and hide(switch) the header in section of tableView by navigation bar button tapped?</p>
  
  <ul>
  <li>Search bar is put in header in section to make it always visible while scrolling tableview</li>
  <li>I would like to hide the search bar in the initial display</li>
  <li>and, show and hide the search bar by tapping navigation bar button</li>
  </ul>
  
  <p>The code and the screenshot are as follows.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>import UIKit
  
  class TableViewController: UITableViewController, UISearchBarDelegate {
  
      let array = ["apple", "orange", "melon", "banana", "peach"]
      let searchBar = UISearchBar()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let searchButton = UIBarButtonItem(title: "Search", style: UIBarButtonItemStyle.plain, target: self, action: #selector(searchTapped))
          self.navigationItem.leftBarButtonItem = searchButton
      }
  
      @objc func searchTapped() {
          // If searchBar is hidden, then show a searchBar.
  
          // If searchBar is shown, then hide a searchBar.
  
      }
  
      override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
          searchBar.delegate = self
          searchBar.placeholder = "Search..."
          searchBar.showsCancelButton = true
          return searchBar
      }
  
      override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -&gt; CGFloat {
          searchBar.sizeToFit()
          return searchBar.frame.height
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
      }
  
      override func numberOfSections(in tableView: UITableView) -&gt; Int {
          return 1
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return array.count
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
          cell.textLabel?.text = array[indexPath.row]
          return cell
      }
  
  }
  </code></pre>
  
  <p><strong>Screenshot</strong></p>
  
  <p><a href="https://i.stack.imgur.com/HgeFg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HgeFg.png" alt="enter image description here"></a></p>