0

All previous questions are relating to anchoring table view section headers, but I would like to know if its possible to anchor the main header below the navigation bar?

I'm using a UISearchController and adding its search bar to the table header, which appears thus:

enter image description here

But because the header is not anchored it and the search bar will disappear if the user scrolls:

enter image description here

Therefore I want to know if its possible to anchor the table header below that navigation bar? Can't find any previous questions relating to this, only relating to anchoring section headers. Fiddling around with the various storyboard settings such as bouncing and scrolling wasn't fruitfull.

P.S. Any proposed solutions involving manually adding a search bar to a section header and anchoring that instead is I just know going to lead to too much hassle and problems when the search bar becomes active. Using UISearchController and adding its search bar to the table view header nicely animates the navigation bar out of the way and slides the search bar up and down as it gains/loses first responder status:

enter image description here

All this is handled automatically by the UISearchController automatically simply by adding one line of code (to add the search bar to the header). Therefore I don't want to go down the road of replacing all the UISearchController provided functionality with custom table section headers and so on.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

1 Answers1

0

Have you considered just putting it outside the UITableView?

I believe that would be the easiest and most straightforward solution.

Edit:

Take a look at this solution: Fixed UISearchBar using UISearchController - Not using header view of UITableView

Edit 2:

I figured it out. Here you go!: https://github.com/gorillatech/TableSearchwithUISearchController


My first answer was actually the one that works the best. You just have to add a UIView above the UITableView.

Then you add the UISearchController's searchBar to said view:

[_searchView addSubview:self.searchController.searchBar];
Community
  • 1
  • 1
guidev
  • 2,695
  • 2
  • 23
  • 44
  • I do not believe putting the search bar outside the table view will be necessarily easy - I will have to add a view between the navigation bar and a table view in order to hold the search bar. Then when the search bar becomes active, remove the navigation bar and slide the view up the screen, increase its dimensions, and also slide the table view up the screen in order to achieve the same functionality provided by UISearchController does when you add its search bar to a table header. Doesn't sound easy and straightforward. Unless there is an easier way than this you had in mind? – Gruntcakes Jun 10 '16 at 18:49
  • @guidev, look at the first picture and the 3rd picture. As Chromium has mentioned, when you add UISearchController.searchBar to a table view's header then the transition between these two is automatically death with. How does your answer address that aspect? – Gruntcakes Jun 10 '16 at 18:52
  • @guidev, adding the search bar to the navigation bar removes the navigation bar title (I will have one, its just not there currently in my screenshot). (I know the navigation bar prompt could be used instead to hold the title, but my GUI requirements are that the title remains the same size and position as for a standard navigation bar, afaik it is not possible to add the search bar to the navigation bar and for the navigation bar to extend in height to accommodate both the search bar and the title). – Gruntcakes Jun 10 '16 at 19:24
  • And as for the second answer in that linked question, that's just a simple fixed view permanently there and the same. I want the behaviour when the navigation bar retracts when the search bar becomes active. I could simple hide the navigation bar when the search bar becomes active but it looks very ugly and not like the UISearchController's provided animations. – Gruntcakes Jun 10 '16 at 19:32