Swift UISeachController add nav bar on selection - ios

I currently have my UISearchController set up so it is presented over the nav bar once the search icon is clicked.
var searchController: UISearchController!
#IBAction func searchMe(sender: UIBarButtonItem) {
// Create the search results view controller and use it for the `UISearchController`.
let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier(SearchResultsViewController.StoryboardConstants.identifier) as! SearchResultsViewController
// Create the search controller and make it perform the results updating.
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = searchResultsController
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.tintColor = Colors.blue
searchController.searchBar.barTintColor = UIColor.whiteColor()
// Present the view controller.
presentViewController(searchController, animated: true, completion: nil)
}
I am using a different searchResultsController than the current table view to display results. The problem is that when a cell is selected in the searchResultsController a segue is performed to a new view controller (the users profile) and there is no navigation bar.
I need a back button to appear to take the user back to the searchResultsController. I have tried a bunch of different segues, setting the nav bars on the storyboard but nothing seems to work. My initial thought was to make this line of code:
searchController = UISearchController(searchResultsController: searchResultsController)
point to a nav controller with searchResultsController as its root view controller but I had no luck. Any help is great! Thank you!

Related

Dont animate navigationBar transition when push ViewController with SearchController and before in navigation were too a VC with SearchController

i have problem with animation of navigation bar when pushing other view controller also with UISearchController to navigation controller. If i push ViewController without SearchController everything is OK. The problem can be seen when on the screen with UITableView scroll down for display UISearchController in navigation bar then push UIViewController too with SearchController to UINavigationController, but on place of UISearchController is only some gray view and the animation of transformation the navigation bar isnt fluent. The gray view disappers after one sec (see image).
This is how it looks like:
This is how I add the UISearchController to navigationBar in viewDidLoad(_:):
private let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
In viewDidLoad(_:) i call only this:
private func setupSearch() {
searchController.searchBar.placeholder = "Suche"
searchController.searchBar.setValue(abbrechen, forKey: cancelButtonText)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = true
}
Do you know where could be problem? Thx for your time.

How to embed a UISearchResultsController View in an existing UINavigationController

I'm creating an iOS 9.3 app using Swift and Xcode 7.3.
It displays a list of food categories (table view). Each category contains food items (collection view). Each item has a simple detail page. This view hierarchy is managed by one navigation controller.
I implemented a global (category-agnostic) food item search bar using UISearchController. The search bar is visible in the category view which, however, uses my food item view to display the results. (The results are items, not categories).
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let ficId = "FoodItemsCollectionControllerId"
let fic = storyboard.instantiateViewControllerWithIdentifier(ficId)
as! FoodItemsCollectionController
searchController = UISearchController(searchResultsController: dic)
fic.searchController = searchController
searchController.searchResultsUpdater = fic
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.sizeToFit()
navigationItem.titleView = searchController.searchBar
searchController.hidesNavigationBarDuringPresentation = false
Problem: Whenever I search for an item and click on it, the food item detail view segue cannot be pushed as the item view is not embedded into my navigation controller. I can present the view modally, with no navigation bar (and no way to go back). I could implement a back button by hand, but I'd like an elegant navigation controller solution.
Tried: Pushing, adding and setting the item view controller(s) manually onto the navigation controller. However, it seems to get ignored whenever I set searchResultsController to it when constructing a UISearchController.
navigationController?.addChildViewController(fic)
navigationController?.pushViewController(fic, animated: true)
navigationController?.viewControllers = [fic, self]
Idea: Use
searchController.presentationControllerForPresentedViewController(*, presentingViewController: *, sourceViewController: *)
I didn't find good documentation and didn't manage to avoid instance-selector errors at runtime though.

Make UISearchBar dismiss like Music App

Notice how the searchBar magnifying glass and placeholder text both shift over on dismissal:
Is there any way I could dismiss this search bar without the text and icon moving over? See the animation of the Music app for the animation I am trying to achieve.
I have a UISeachBar that gets presented when the search button is clicked in a nav bar.
#IBAction func searchButtonPressed(sender: AnyObject) {
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
presentViewController(searchController, animated: true, completion: nil)
}
Now when the cancel button is pressed, I want the whole view to move up without seeing the SearchBar icon and placeholder text slide to the center of the searchBar as the view moves up off screen.
I know there is the method searchBarCancelButtonClicked(searchBar:), but this seems to automatically dismiss the SearchBar and I don't know how to control that.
How can I make the SearchBar dismiss like the one in the Music app?
EDIT: Here is the only delegate method I am using:
func updateSearchResultsForSearchController(searchController: UISearchController) {
masterFilter.removeAll()
filterContentForSearchText(searchController.searchBar.text!)
tableView.reloadData()
}
This is how I have achieved this. It is simple and easy.
extension UIViewController {
func setNavigationBarItem() {
let searchaButton = UIButton.init(frame: CGRectMake(0, 0, 30, 30))
searchaButton.setBackgroundImage(UIImage(named: "search.png"), forState: UIControlState.Normal)
searchaButton.addTarget(self, action: #selector(self.searchPressed), forControlEvents: UIControlEvents.TouchUpInside)
let rightButton: UIBarButtonItem = UIBarButtonItem.init(customView: searchaButton)
navigationItem.rightBarButtonItem = rightButton
}
public func searchPressed(){
var searchController: UISearchController!
// Create the search results view controller and use it for the UISearchController.
let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier("SearchResultController") as! SearchResultsViewController
// Create the search controller and make it perform the results updating.
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = searchResultsController
searchController.hidesNavigationBarDuringPresentation = false
// Present the view controller.
presentViewController(searchController, animated: true, completion: nil)
}
}
Note:
This code is for old swift version.
I hope this will help you.
The searchBar expands to the right because the search is being cancelled, and the cancel button is disappearing.
Instead of canceling the search and triggering that animation, simply explicitly dismiss the search controller.
You can do this by setting
searchController.active = false
To make this animation work, you'll need to understand custom UIViewController transitions. Within the UISearchBarDelegate method -searchBarCancelButtonClicked(searchBar:), you'll need to do a few things:
Hide the UINavigationBar on the UISearchController dismissal
Animate the UITableView to the bottom of the frame and fade out.
If you're presenting search modally, ensure that you're setting the modalPresentationStyle to UIModalPresentationCurrentContext.

Error "Application tried to present modal view controller on itself" while activating UISearchController on action

In my code this is how I setup UISearchController:
searchResultController = storyboard!.instantiateViewControllerWithIdentifier(DBSearchResultControllerIdentifier) as! DBSearchResultController
searchController = UISearchController(searchResultsController: searchResultController)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchResultController.tableView.tableHeaderView = searchController.searchBar
on some action all I do is:
#IBAction func cityButtonTapped(sender: UIButton) {
searchController.active = true
}
But then I have an error:
Application tried to present modal view controller on itself. Presenting controller is UISearchController: 0x7f9a0c04a6a0
The apple documentation for UISearchController clearly says the following things:
Setting active property to YES performs a default presentation of
the search controller.
Set the searchResultsController parameter to nil to display the
search results in the same view that you are searching.
So it looks like you are using your current view controller itself as your searchResultsController and hence when you try to set active to YES, it tries to modally present the your current view on itself and hence the error.

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

Resources