If I embed the ViewController into a Navigation Bar, navigationItem.titleView.resultSearchController?.searchBar will put a search bar into the navigation bar. However, I've created a UISearchController and a UINavigationBar with code. This time, the navBar is showing up, but the searchBar isn't.
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.delegate = self
let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y:0, width: 375, height: 64))
self.view.addSubview(navBar)
//navBar.topItem = resultSearchController?.searchBar
self.navigationItem.titleView = resultSearchController?.searchBar
navBar.topItem = resultSearchController?.searchBar doesn't work because topItem is a String value and resultSearchController?.searchBar is a UIView type. How can I achieve the same effect?
Create a UINavigationItem instance and add it to the created navigation bar.
Add the search controller search bar to the UINavigationItem as titleView.
class SearchViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.addNavigationbar()
}
func addNavigationbar() {
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60))
self.view.addSubview(navBar)
let navigationItem = UINavigationItem(title: "")
self.searchController = searchControllerWith(searchResultsController: nil)
navigationItem.titleView = self.searchController.searchBar
navBar.setItems([navigationItem], animated: false)
self.definesPresentationContext = true
}
func searchControllerWith(searchResultsController: UIViewController?) -> UISearchController {
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = true
return searchController
}
class UIViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
func updateSearchResults(for searchController: UISearchController) {
}
}
Related
I got this dark background:
image of the problem
Here are my codes:
class SearchController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self as? UISearchResultsUpdating
self.definesPresentationContext = true
self.navigationItem.titleView = searchController.searchBar
searchController.hidesNavigationBarDuringPresentation = false
self.definesPresentationContext = true
}
}
What should I do?
Thanks!
Try adding this
searchController.dimsBackgroundDuringPresentation = false
https://developer.apple.com/documentation/uikit/uisearchcontroller
I programmatically created a search bar in ViewController.
But when I scroll up the screen, the search bar goes up.
How can I freeze the search bar?
class searchbarTable: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
#IBOutlet var tableview: UITableView!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
self.tableview.tableHeaderView = searchController.searchBar
}
}
You can try this and set searchbar in Navigation Bar
self.navigationController?.searchDisplayController = searchController
OR
lazy var searchBar = UISearchBar(frame: CGRectZero)
override func viewDidLoad() {
super.viewDidLoad()
searchBar.placeholder = "Search"
navigationItem.titleView = searchBar
}
OR
var leftNavBarButton = UIBarButtonItem(customView:Yoursearchbar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
OR
searchController.hidesNavigationBarDuringPresentation = false
it hide naviationbar when we present okay
Hope it helps You :)
When I assign a TableViewController as the constructor for UISearchController, when the table is loaded, it hides the navigation bar (that includes title and search bar). How can I make it, so it doesn't hide it?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationItem()
}
}
extension ViewController {
private func configureNavigationItem() {
let resultsController = UITableViewController(style: .plain)
navigationItem.searchController = UISearchController(searchResultsController: resultsController)
navigationItem.hidesSearchBarWhenScrolling = false
}
}
Initial Load
Tap Any Key
Found the answer:
definesPresentationContext = true
as in:
private func configureNavigationItem() {
let resultsController = UITableViewController(style: .plain)
navigationItem.searchController = UISearchController(searchResultsController: resultsController)
navigationItem.hidesSearchBarWhenScrolling = false
**definesPresentationContext = true**
}
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
}