Display searchController on buttonClick - ios

i'm trying to show a searchController on buttonClick. However it seem to create an error when the button is clicked. I get an error on the presentViewController row. What am i doing wrong in order to achieve this?
by the way i'm having this code in a tableViewController
func searchButtonClicked(sender: UIBarButtonItem) {
searchController = UISearchController(searchResultsController: self)
// searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
self.presentViewController(self.searchController, animated: true, completion: nil)
}

Related

Whole app freezes, when opening UISearchController in NavigationBar

I'm making an app with a list of crypto-currencies. There should be an ability to make a search within those 3 filtered lists (BTC, ETH, USD). The only problem i have is with the search bar.
As soon as i press on the "magnifying glass" icon, the search bar presents, but the whole app freezes. I'm not able to press "Cancel", type in the bar, close it and move the UITableView. I don't understand the reason of such behavior.
var searchController : UISearchController!
#IBAction func searchingButton(_ sender: Any) {
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = "All currency pairs"
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchResultsUpdater = self
definesPresentationContext = true
navigationItem.searchController = self.searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchButton.isHidden = true
present(searchController, animated: true, completion: nil)
}
extension ViewController: UISearchControllerDelegate {
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
navigationItem.searchController = nil
navigationController?.view.setNeedsLayout()
navigationController?.view.layoutIfNeeded()
self.indexChange(self.segmentedControl!)
}
}
Please help me with this problem. If needed, more code will be provided.
Well, it's weird, but cleaning the project and system reboot helped.

UISearchController appears behind NavigationBar

I'm trying to show the UISearchController by tapping on the UIBarButtonItem. But the SearchBar appears behind the NavigationBar. If I set definesPresentationContext = false, then the SearchBar appears above the NavigationBar, but the transitions don't work.
#IBAction func searchButtonPressed(_ sender: Any) {
searchResultsController = self.storyboard?.instantiateViewController(withIdentifier: "SearchResultsController") as? SearchResultsController
searchResultsController.tableView.delegate = self
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = searchResultsController
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.autocapitalizationType = .words
definesPresentationContext = true
present(searchController, animated: true)
}
Before BarButtonItem tapped
After BarButtonItem tapped
View UI Hierarchy
Interface Builder
searchController.hidesNavigationBarDuringPresentation = false solve this problem, but I need the navigation bar to not be hidden
UPDATE:
Inserting the SearchBar inside the Navigation Bar is not suitable for my app
https://imgur.com/a/74e5sSk
If you are using UITableViewController then you can do the following:
let searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
searchController.definesPresentationContext = true
Then, you need to assign this searchController to navigationItem:
self.navigationItem.searchController = searchController
Now searchbar is going to appear when you pull the tableview and will look this way:
If you want to keep the search bar permanently, then do this:
self.navigationItem.hidesSearchBarWhenScrolling = false

Swift iOS -How to get keyboard to present itself at the same exact time a SearchController is presented?

Just for context my keyboard successfully appears so that's not the problem.
I have a searchButton as my rightBarButtonItem, when pressed it modally presents a vc that contains a SearchController. When the SearchController is presented the keyboard is also presented but the keyboard appears a second late, there's like a 1 second delay before it shows itself. Basically the vc appears on the scene and then the keyboard appears afterwards, I cannot get the keyboard to appear at the same time the SearchController is presented. I was on YouTube's and Vimeo's iOS apps and when I pressed their search button the keyboard is presented with the SearchController at the same exact time, there isn't a 1 second delay.
How can I get the keyboard to present itself at the same time the SearchController is presenting itself?
button to modally present SearchController:
#objc func searchButtonTapped() {
let searchVC = SearchController()
let nav = UINavigationController(rootViewController: searchVC)
present(nav, animated: true, completion: nil)
}
SearchController:
I've already tried adding searchController.isActive = true and searchController.searchBar.becomeFirstResponder() in DispatcQeue.main in viewWillAppear and viewDidAppear and it made no difference
class SearchController: UIViewController {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.showsCancelButton = true
searchController.searchBar.placeholder = "Search"
searchController.searchBar.returnKeyType = .search
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.sizeToFit()
searchController.searchBar.tintColor = UIColor.black
definesPresentationContext = true
navigationItem.hidesBackButton = true
navigationItem.titleView = searchController.searchBar
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
searchController.isActive = true
}
// I tried both of these searchContrller delegate methods SEPERATELY but it made no difference, there's still a 1 second delay
func presentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
func didPresentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}
}
Sure there is a delay.. ones this animation is completed, then keyboard appears.
present(nav, animated: true, completion: nil)
Please try this.
It will immediately open the keyboard if you would not preset view controller with an animation but if we present view controller animation it opens keyboard after the present animation is finished.
Thanks.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
DispatchQueue.main.async {
self.searchController.searchBar.becomeFirstResponder()
}
}

UISearchController incorrect behavior when it is present modally

I showed UIViewController like modal style. UIViewController has UITableView. I added UISearchController to UITableView but UISearchController has incorrect behavior when it disappeared. I made many times UISearchControllers but I didn't face the this behavior.
My code
So I show the UIViewController
#objc fileprivate func selectInterlocutor(_ button: UIButton) {
let selectInterlocutorTVC = SelectInterlocutorTableViewController()
selectInterlocutorTVC.modalPresentationStyle = .overCurrentContext
selectInterlocutorTVC.modalTransitionStyle = .coverVertical
selectInterlocutorTVC.providesPresentationContextTransitionStyle = true
present(selectInterlocutorTVC, animated: true) {
}
}
I add UISearchController to tableView
fileprivate func addSearchController() {
searchController.searchBar.barStyle = .default
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.autocapitalizationType = .words
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
}
Please look at this video that you can see this behavior.
https://www.dropbox.com/s/l3m3q8wmqoy3qv2/SearchBarBug.mov?dl=0
How can I fix it?
I removed UISearchController and I used UISearchBar and it works for me.
fileprivate func addSearchController() {
searchBar.barStyle = .default
searchBar.delegate = self
searchBar.autocapitalizationType = .words
searchBar.showsCancelButton = true
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
}

Change height of UISearchController view height in iOS 8?

I have a UISearchController added onto my view, but I would like to change the height of it. This is what I have:
func initSearch() {
// Create the search controller and make it perform the results updating.
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = true
searchController.searchBar.tintColor = UIColor.whiteColor()
searchController.searchBar.barStyle = .Black
searchController.searchBar.scopeButtonTitles = [ "Any", "Title", "Content", "Keywords" ]
self.definesPresentationContext = true
}
#IBAction func searchTapped(sender: AnyObject) {
// Present the view controller.
presentViewController(searchController, animated: true, completion: nil)
}
Below is a screenshot of what it looks like. I would like trim some space from the height though. How can this be done?

Resources