I put my UISearchController's search bar on navigation bar's titleView. But when I tap to search, UISearchBar does not respond. What can be the cause ?
let tvController:UITableViewController = UITableViewController()
tvController.tableView.delegate = self
tvController.tableView.dataSource = self
self.searchController = UISearchController(searchResultsController: tvController)
self.searchController?.searchResultsUpdater = self
self.searchController!.searchBar.placeholder = "Search for places"
self.searchController!.hidesNavigationBarDuringPresentation = false
self.searchController!.dimsBackgroundDuringPresentation = false;
self.searchController!.searchBar.searchBarStyle = UISearchBarStyle.Prominent
self.searchController!.delegate = self
self.searchController!.searchBar.delegate = self
self.definesPresentationContext = true
self.searchController!.searchBar.sizeToFit()
self.navigationItem.titleView = self.searchController!.searchBar
This might help
Replace
self.searchController = UISearchController(searchResultsController: tvController)
With
self.searchController = UISearchController(searchResultsController: nil)
Related
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
There is a code that creates the Search Bar. After there is a click on this Search Bar, then it is shifted down along with the "Cancel" button. I enclose screenshots and code.
before click
after click
#IBAction func showSearchBar(_ sender: UIBarButtonItem) {
self.navigationItem.setRightBarButtonItems([], animated: true)
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
self.searchController.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.obscuresBackgroundDuringPresentation = true
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.placeholder = "Искать продукты"
self.searchController.searchBar.setShowsCancelButton(true, animated: true)
self.searchController.searchBar.searchBarStyle = .minimal
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
How to fix it?
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
I have a problem regarding the UISearchController component. I have set up a searchcontroller like this:
let searchResultsController = TSSearchResultsController(nibName: "TSSearchResultsController", bundle: nil)
searchResultsController.delegate = self
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
// SearchController
searchController.searchResultsUpdater = searchResultsController
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
// SearchBar
searchController.searchBar.delegate = searchResultsController
searchController.searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchController.searchBar.tintColor = IDAssetManager.getColorWhite()
searchController.searchBar.sizeToFit()
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.showsCancelButton = false
self.definesPresentationContext = true
...
When I click the search result I push a view onto the navigationstack which is defined in the same place I initialise the searchController. When I come back, the searchController view has been pushed down a few a bit. This is continuously happening every time I push and pop from navigationController. I have tried self.extendedEdgesForLayout = [] but does not help.
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