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?
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
I have a problem with my status bar .
it disappears as soon as i press on my search bar,Look at the examples :
Status bar shows normally before search bar pressed:
Status bar does not show and just disappears out of view:
Thanks ahead ! :)
here is my searchController setup :
override func viewDidLoad() {
super.viewDidLoad()
definesPresentationContext = true
self.resultsController.tableView.dataSource = self
self.resultsController.tableView.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController = UISearchController(searchResultsController: self.resultsController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = true
self.searchController.searchBar.sizeToFit()
self.searchController.searchBar.barTintColor = UIColor.blackColor()
self.searchController.searchBar.endEditing(true)
self.searchController.searchBar.placeholder = "חפש ברים"
}
Try this setting:
searchController.hidesNavigationBarDuringPresentation = false
And override this function (Swift 2):
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
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
}
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)
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?