How to make search bar not disappear while scrolling in table view? - ios

I want to make my search bar stop scrolling along with table view. I tried to add search controller into viewForHeaderInSection and it failed.Also I want to try to show original content while search bar active. So I try to do it in viewDidLoad but I don't know how to make it work. Here is my code for both of method.
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
Do you have any suggestion?

Add both the table view and the search view into another view (which becomes the view of your view controller). Then the search view will be completely disconnected from your table view.
Note that if your view controller is currently a table view controller subclass you will need to change that to be a simple view controller subclass.

You are adding the searchbar to the header view. If you want to keep it above your table you can add it to the navigation controller view. How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?

Related

iOS 11 tableView with searchBar issue

Please help to determine the reason of such behavior (it's hard to describe by words, so I recorded the short video) https://youtu.be/E2ks0liFX4I
In short words:
Initially it's able to scroll content beneath navigation bar. If press search field - the search bar looks like detached from table view and goes too high and overlapped by status bar (look at increased space between grey border of search bar and first row in the table). After Cancel button pressed - the search bar jumps down and now can't be hided by scrolling.
I'm using UITableViewController.
Search bar initialization in viewDidLoad:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
And in StoryBoard:
tableView settings
Not sure what u want. I guess u want the search bar to hide when scrolling?
I set searchbar to the navigationItem, then set the hidesSearchBarWhenScrolling property.
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true

First row hidden when table view appears while search controller is active

I'm using UISearchController with a UITableViewController. When I have a search in progress, and I select a row in the table to go to the detail view, and then go back to the table view, the first row of the table is hidden behind the search bar.
I can scroll the first row back into view, but then it just pops back behind the search bar when I let go of the scroll. So frustrating!
In viewDidLoad I have:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
mainTable.tableHeaderView = searchController.searchBar
Removing definesPresentationContext = true solves the problem, but I need that to prevent the search bar from showing up if the user segues to other view controllers.
I've spent hours searching for a solution and trying different ways to fix the problem.
Is there any way to set the first row of the table so it is below the search bar when the table view re-appears?
I tried setting definesPresentationContext = true in prepareForSegue, but that did not work.
I found a way to solve this problem.
Do not set definesPresentationContext = true
In prepare(for:sender:), add the following. This keeps the search active, but prevents the search bar and keyboard from appearing on the view controller being segued to.
searchController.searchBar.isHidden = true
searchController.searchBar.endEditing(true)
In viewWillAppear, add the following. This makes the search bar visible when the view controller with the search controller comes to the top of the navigation stack. The first row of the table is positioned properly below the search bar.
searchController.searchBar.isHidden = false
In willMove(toParentViewController:), add the following. This cancels any active search, preventing the search bar from appearing on the view controller coming to the top of the navigation stack.
if parent == nil {
searchController.isActive = false
}
This is working for me:
self.extendedLayoutIncludesOpaqueBars = true

Ios/swift / UISearchController or SearchDisplayController with custom cell

I currently have an app with a search icon / button at the top in the navigation bar.
I'm not sure if it would be possible with storyboard but i would like to display a search controller that appears with a custom table view and a close button, a lot like the wikipedia app
see examples below
http://i.imgur.com/ggsTj6W.png
http://i.imgur.com/BVDLU2O.jpg
So workflow would be:
- Press the search icon,
- Table view comes up with search focused with a close button
- Search results come in the table view as custom cells from a re-usable source
When I use this search on the wikipedia app it feels very native to ios so I assume there is a way to do it?
thank you
Create a UITableViewController object and conform it to protocol UISearchResultsUpdating
Add the search controller to class
let searchController = UISearchController(searchResultsController: nil)
Add the following code to viewdidload method
self.definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchController.searchBar
Implement the following method to conform to UISearchResultsUpdating protocol
func updateSearchResultsForSearchController(searchController: UISearchController)
On the main page (where you want to show the search button), create a segue to the above tableViewController and set it to present modally when button is tapped.

How to make search bar not scrolling along with table view? [duplicate]

This question already has an answer here:
iOS7: How to add a fixed Search Bar like Contacts on iOS7?
(1 answer)
Closed 7 years ago.
I want to make a search bar like this.
The search bar in contacts app is stay while I scroll down. I want to make a search bar with this property that It doesn't scroll down along with table view . I did my app in table view controller.This is my code in my table view controller.
myTableViewController(in my viewDidLoad function):
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
It should work like search bar in contact because of this code
tableView.tableHeaderView = searchController.searchBar
Is there something i do wrong? If not then how to make my search bar doesn't scroll down with table view(in table view controller)?
Instead of a search bar in a table view, make a search bar and a table view. Thus, the search bar just sits there while the table view scrolls.

UISearchController persisting after segue

I have an app with a UISearchController. This element of the UI is completely set up in code like this:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, searchController.searchBar.frame.size.width, 44.0)
I am then adding it to my tableView's tableHeaderView
tableView.tableHeaderView = searchController.searchBar
Everything seems to be working fine, but when it's active and I select an item in my tableView, my app segues to another view controller with the search controller persisting in the view. I'm unsure as to how this is possible since the search controller should be a subview of the table view in another view controller. How can I prevent this from happening?
You can hide the searchController manually by setting the active property to false in prepareForSegue. Add the below code in prepareForSegue()
searchController.active = false
Alternatively, you should add the following line in viewDidLoad() to get the default behaviour
definesPresentationContext = true
From the documentation for definesPresentationContext
A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.
Discussion
When a view controller is presented, iOS starts with the presenting view controller and asks it if it wants to provide the presentation
context. If the presenting view controller does not provide a context,
then iOS asks the presenting view controller's parent view
controller. iOS searches up through the view controller hierarchy
until a view controller provides a presentation context. If no view
controller offers to provide a context, the window's root view
controller provides the presentation context.
If a view controller returns true, then it provides a presentation
context. The portion of the window covered by the view controller's
view determines the size of the presented view controller's view.
The default value for this property is false.
Important note (from #paulvs in the comments)
Little gotcha. Set definesPresentationContext on the view controller, not the search controller, I think this is worth emphasising.
If you manage your own transitions and use popToViewController to leave the view, provide the context on the searchController instead of the view
searchController.definesPresentationContext = true
or you will get an error
popToViewController:transition: called on <UINavigationController 0x7f984204f800> while an existing transition or presentation is occurring; the navigation stack will not be updated

Resources