2

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?

PiterPan
  • 1,760
  • 2
  • 22
  • 43

2 Answers2

1

You need to set extendedLayoutIncludesOpaqueBars to true in viewDidLoad().

self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
sandy
  • 344
  • 3
  • 12
  • It works! thx! but I have another problem. What I need to do when I click in Result list and move into new view and `searchbar` don't want to disappear? I try with it `func searchBarResultsListButtonClicked(searchBar: UISearchBar) { self.navigationController?.navigationBarHidden = true }` but doesn't work. – PiterPan Jan 06 '16 at 12:57
  • Did you try to self.definesPresentationContext = true ? – Sébastien Polytech' Dec 29 '17 at 13:11
-1

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()
Rahul
  • 5,594
  • 7
  • 38
  • 92