Search Bar Animation Override - ios

I am in the middle of implementing search into a tableView based app. I have currently implemented it using a UIView in Storyboard (which I have placed in the tableHeaderView) with the following code:
resultsTableController = SearchResultsTableViewController()
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
tableViewSearchBarView.addSubview(searchController.searchBar)
I am currently getting this jerky animation: https://imgur.com/a/7h0J15e
Can I create a custom animation that will happen when the search bar is clicked and cancelled and override this animation somehow? I can't use the normal:
navigationItem.searchController = searchController
as I need to have the Title in a custom tableHeaderView and not in a navigationBar.
How could I go about doing this? I am just wanting a normal animation like what is in the stock Apple Notes App.

Related

UISearchBar in UINavigationController

I am wondering to programmatically put a UISearchBar into UINavigationController, rather than replacing UINavBar title, as I want the large title to show up. See Files app on iOS.
Here is my .swift
class Search: UITableViewController, UISearchBarDelegate
override func viewDidLoad() {
super.viewDidLoad()
searchBar()
}
func searchBar() {
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.searchController?.searchBar.placeholder = "search".localized
navigationItem.searchController?.dimsBackgroundDuringPresentation = false
navigationItem.hidesSearchBarWhenScrolling = false
}
The point is: by changing nil to actual searchResultsController, after typing text into search bar, the whole navigation controller (with search bar and typed text) got replaced by the searchResultsController view. No search bar while typing into it in short.
I am trying hard to use iOS 11/12 native look and feel within the app and putting the search bar into TableViewController is a no way here. Can someone help me?
If you don't need a different view controller to display the results simply use (it will not make your navigation bar disappear):
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
And do not change the value after that.
Most of your code is good, however you will need to implement the UISearchResultsUpdating protocol to detect updates (when the user write in the search bar) and use a table view (inside your view controller) to display the results of the search.
You do that by reloading the data of your table view inside the function: updateSearchResults(for searchController:) after filtering the data you want with the text inside the search bar. You can read this good tutorial on the subject for more information.

Custom UITableViewController for UISearchController overlays the search bar in iOS 11

Custom UITableViewController for UISearchController overlays the search bar in iOS 11 when you set the search controller to the navigationItem.searchController. The table view controller covers the whole screen so you cannot see the search bar while typing. This is not an issue when you send nil to UISearchController. Basically, I have a search bar for this map app, so I am setting a UITableViewController to the UISearchController to handle the display of the search result when searching for location. Previously, the table view shows up at the bottom of the search bar, now it covers the whole screen.
Here's a code snippet:
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchBar.sizeToFit()
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = true
searchController.definesPresentationContext = false
searchController.searchResultsUpdater = self
searchController.delegate = self
if #available(iOS 11.0, *){
self.navigationItem.searchController = searchController
}
Found the issue. Need to set the right origin value in willPresentSearchController

How do I set the UISearchController's searchBar to a view that isn't the tableHeaderView or navigationItem.titleview?

I'm trying to keep the search bar in view as the table scrolls. At the moment I'm placing it as the header in a tableview, and it works as it should, but of course the search bar scrolls off screen as you go down the table. I thought I could do this simply modifying this code sample:
How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
tableview.tableHeaderView = searchContoller.searchBar // How to put it elsewhere?
//Alternative that also works
navigationItem.titleView = searchController.searchBar
The idea was to take some other view and do
otherview = searchController.searchBar
For instance an outlet to a UISearchBar, or a blank UIView, or something like that.
But it doesn't show the searchBar if I do that. It seems to only work as the header view of a table or as a navigationItem.titleView.
Am I missing something?
If you have a blank UIView that is placed above the tableview.
let's assume you have an outlet to that blank UIView called searchContainer.
Then you can add the search bar of the UISearchController to that view by adding the following line
searchContainer.addSubview(searchController.searchBar)

UISearchBar in UISearchController Does Not Disappear

I AM NOT USING STORYBOARD SEGUES.
When I use UISearchController to have a search bar at the top of my table view, I get extremely strange behavior. Is there documentation on how I'm supposed to handle the search bar when a view dismisses? When I switch to a new view controller via an animation or push one on a nav stack, the bar is stuck in its spot until I hit the "Cancel" button.
See a video of what's happening in this short clip:
https://www.youtube.com/watch?v=6G7xFMENm_o&feature=youtu.be
This is the code, in the view controller, that sets up search bar:
var s: UISearchController!
private func configureSearching() {
s = UISearchController(searchResultsController: nil)
s.searchResultsUpdater = self
s.dimsBackgroundDuringPresentation = false
s.searchBar.searchBarStyle = .Minimal
s.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = s.searchBar
s.searchBar.sizeToFit()
}
you should add the following line in viewDidLoad
self.definesPresentationContext = true

Use UISearchController without storyboard

I am relatively new to iOS (100 hours or so) and I am trying to implement a UISearchController without a storyboard. Is it possible to do this programmatically?
You have to implement UISearchController programmatically since Apple hasn't provided the ability to set it up using Storyboard yet.
Here are the steps to implement UISearchController in a view controller
Conform to UISearchResultsUpdating Protocol
Create a variable to reference the UISearchController
var searchController: UISearchController!
Set searchController in viewDidLoad(_:)
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
// The object responsible for updating the contents of the search results controller.
searchController.searchResultsUpdater = self
// Determines whether the underlying content is dimmed during a search.
// if we are presenting the display results in the same view, this should be false
searchController.dimsBackgroundDuringPresentation = false
// Make sure the that the search bar is visible within the navigation bar.
searchController.searchBar.sizeToFit()
// Include the search controller's search bar within the table's header view.
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
}
Implement the method updateSearchResultsForSearchController(_:), which is called when the search bar becomes the first responder or when the user makes changes to the text inside the search bar
func updateSearchResultsForSearchController(searchController: UISearchController) {
// No need to update anything if we're being dismissed.
if !searchController.active {
return
}
// you can access the text in the search bar as below
filterString = searchController.searchBar.text
// write some code to filter the data provided to your tableview
}

Resources