UISearchController in navigationBar - ios

I'm trying to use the new UISearchController in my tableViewController.
However I'm a bit confused on how it can move up in the navigationController when I press the searchBar like it did with the old searchDisplayController?
At the moment it just stay in the tableHeader.
Here is my code:
self.teamSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.showsScopeBar = true
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
Controller:
When I click on searchbar:

You can place the UISearchBar of UISearchController in the navigation bar instead of table header
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;

Swift version:
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar
self.definesPresentationContext = true

It has to do with the navigation bar.
func willPresentSearchController(searchController: UISearchController) {
self.navigationController?.navigationBar.translucent = true
}
func willDismissSearchController(searchController: UISearchController) {
self.navigationController?.navigationBar.translucent = false
}
If you do this, then you will be able to add it to the table view header AND get the baked-in search controller animation!

Related

How to fix jumping upon clicking search bar?

I've tried to add a simple search controller but whenever I'm clicking on it so it becomes active, it jumps up, out of screen. Why is that even happening? I checked all the code and I'm not manipulating with constraints or anything.
I initialize my search controller through the following function (and call it in ViewDidLoad):
private func initSearchController() {
self.searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.searchBar.delegate = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.barStyle = UIBarStyle.black
controller.searchBar.barTintColor = UIColor.white
controller.searchBar.backgroundColor = UIColor.clear
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
self.searchController.hidesNavigationBarDuringPresentation = false

Cannot Interact with SearchBar added as HeaderView

I have added SearchController's searchbar to tableview controller's headerview but when I tap on the searchbar keyboard doesn't pop up. it feels like there is no user interaction. Following is the code used to add searchbar to uitableview.
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.userInteractionEnabled = true
dispatch_async(dispatch_get_main_queue(),{
self.tableView.tableHeaderView = controller.searchBar
controller.searchBar.bounds = (self.tableView.tableHeaderView?.bounds)!
controller.definesPresentationContext = true
self.definesPresentationContext = true
})
return controller
})()
Try to add just this 5 lines of code in your viewDidLoad and give it a try
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
self.tableView.tableHeaderView = searchController.searchBar

Blank space between SearchBar and NavigationBar - Swift

I have a simple tableViewController, a searchBar with scopes and a navigationBar.
When I click on my searchBar I see my scopes and all works. Then I click on a row and I go to my DetailPage and the search bar isn't hidden (I don't know why).
So I click cancel and then I return to my tableView. When I return to my tableView there is a blank space between my SearchBar and my NavigationBar.
This is my code:
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = true
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.scopeButtonTitles = ["Title", "Author", "Location", "Price", "User"]
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
func updateSearchResults(for searchController: UISearchController)
{
let scopes = resultSearchController.searchBar.scopeButtonTitles
let currentScope = scopes![resultSearchController.searchBar.selectedScopeButtonIndex] as String
filterContentForSearchText(searchText: searchController.searchBar.text!, scope: currentScope)
}
try to Add
self.automaticallyAdjustsScrollViewInsets = false;
self.extendedLayoutIncludesOpaqueBars = true
in viewDidload.

swift search Bar and navigation bar will be hidden after starting

in my swift 2 app i have a table view with a search bar:
But if tap on the search bar, my navigation bar and the search bar will be hidden.
This is my code which is in the viewDidLoad
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.hidesNavigationBarDuringPresentation = true
self.MyTable.tableHeaderView = controller.searchBar
return controller
})()
At the beginning i also get this message:
Attempting to load the view of a view controller while it is
deallocating is not allowed and may result in undefined behavior
()
My Question is, where is my mistake? :/
from this tutorial you can use this code to solve the problem.
self.navigationController!.navigationBar.translucent = false
searchController!.hidesNavigationBarDuringPresentation = false
// This makes the view area include the nav bar even though it is opaque.
// Adjust the view placement down.
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = UIRectEdge.Top
You have to modify your code with the below if you want to make it worked perfectly:
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.searchBar.delegate = self
self.definesPresentationContext = true
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.hidesNavigationBarDuringPresentation = true
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.resultSearchController
} else {
self.tableView.tableHeaderView = self.resultSearchController.searchBar
}

UISearchController makes the controller black

I am using UISearchController in iOS 8 with the following intializaiton in viewDidLoad of a view controller embedded in a tab controller
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchBar = _searchController.searchBar;
[_searchController.searchBar sizeToFit];
_searchController.searchBar.delegate = self;
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;
_shopsTable.tableHeaderView = _searchController.searchBar;
I've implemented
- (void) updateSearchResultsForSearchController:(UISearchController *)searchController and (void)filterContentForSearchText:(NSString *)searchText
and the search works, the tableview gets updated properly, etc.
But!
If I switch tabs while the searchcontroller is active (just tapping the search bar or with some text) to a different tab, and then go back to teh search tab, I get a blank screen with only the search bar, like this
In this case, I search for things that start with lar, which did return results and displayed them correcly. But if I switch tabs, and return to the search tab I get a blank screen like this. The only way the controller returns to its original state is if I do _searchController.active = NO. But if the user wants to keep that search active, I can't just deactivate it.
I am sure I am missing something, but since there isn't much to do in UISeachController, I can't figure out what is causing this..
Try self.definesPresentationContext = YES; instead of NO. Here is how I setup my UISearchController, but I haven't done it this way in a UITabBarController before.
func setupSearchController() {
let resultsController = UIStoryboard(name: "ATPageTableViewController", bundle: nil).instantiateViewControllerWithIdentifier("ATPageTableSearchResultsViewController") as! ATPageTableSearchResultsViewController
searchController = UISearchController(searchResultsController: resultsController)
searchController.delegate = self
resultsController.delegate = self
resultsController.cellIdentifier = ATDataSetItemTableViewCellIdentifier;
resultsController.table = self.table!
searchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
definesPresentationContext = true
}
Write the below code in viewDidLoad mehtod.
self.definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = true

Resources