Can't focus searchBar in searchController in UINavigationBar - ios

like in topic - I can't focus searchBar despite that i set this delegate and active.
Here is my code from viewdidLoad():
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self as? UISearchControllerDelegate
searchController.searchBar.delegate = self
searchController.isActive = true
self.definesPresentationContext = false
let newSearchBar = searchController.searchBar
Also Ive set var searchBar: UISearchBar! and searchBar = newSearchBar
and it's look like that in app: https://i.stack.imgur.com/uL6ds.png
Anyway, when I click in this searchbar then click TAB on my keyboard then im able to write something...

Related

How to set SearchBar text field text

I am setting UISearchController as following
navigationItem.searchController = searchController
In results UIViewController, I need to set search text when button clicked
this code doesn't work
navigationController?.navigationItem.searchController?.searchBar.searchTextField.text = searchText
You need to have a reference of your searchController as a property. For example, you have a property:
lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.backgroundColor = .white
return searchController
}()
and then assigning that property to navigationItem like what you did:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
}
and then finally, setting search text when a button is clicked:
self.searchController.searchBar.text = "πŸ»βœ…πŸ’―πŸ₯΄πŸ‡΅πŸ‡­"
OR SIMPLY:
self.navigationItem.searchController?.searchBar.text = "πŸ»βœ…πŸ’―πŸ₯΄πŸ‡΅πŸ‡­"
No need to get these from your navigationController.

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

How to fix programmatically the search bar in swift

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 :)

iOS - SearchBar jumping to top as i tap on it to enter text

override func viewDidLoad() {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
self.tableView?.tableHeaderView = searchController.searchBar
}
This is default behavior when you add the search bar to tableview header.

How to add scope buttons in a UISearchController embedded in UINavigationController

I have an App that is presenting a MKMapView embedded in a UINavigationController. In the UINavigationController I have put a UISearchController. When the User touch the UISearchController it displays a UITableViewController.
It works well while I'm not adding the Scope button in the UISearchController.
Here the screenshot of the UISearchController in the UINavigationController when I start the App.
Next when I touch the UISearchController, it displays the UITableViewController and scope button.
Here we can already see there's an issue with the scope button because they are not well integrated in the UISearchController (color should be translucent)
Next, when I touch the Cancel button to go back to the Main viewController, the UISearchController is not recovering its original style
it has a dark gray border (that probably comes from the scope button).
Here's how I add the UISearchController in the Main view Controller
func initSearchController() {
let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController
self.searchController = UISearchController(searchResultsController: mySearchController)
mySearchController.theSearchController = self.searchController
mySearchController.delegate = self
// Configure the UISearchController
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.placeholder = "data.."
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
this method is called in the viewDidLoad() of my Main ViewController.
Next, when the SearchController is displayed, I'm adding the scope button with the following code in my TableViewController subclass
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Mandatory to make sure the TableView is displayed when the search field is empty
// when user touch it.
view.hidden = false
var rect = delegate.searchController.searchBar.superview?.frame
rect?.size.height = 88
self.delegate.searchController.searchBar.scopeButtonTitles = ["one", "two", "three"]
self.delegate.searchController.searchBar.showsScopeBar = true
self.delegate.searchController.searchBar.superview?.frame = rect!
}
and the following code is executed when search is closed
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
var rect = delegate.searchController.searchBar.superview?.frame
rect?.size.height = 44
self.delegate.searchController.searchBar.superview?.frame = rect!
self.delegate.searchController.searchBar.showsScopeBar = false
self.delegate.searchController.searchBar.scopeButtonTitles = nil
}
As you can see I have severals issues with this code.
Scope buttons are not displayed correctly and I'm unable to add them with a nice animation
When user exits the search Scope buttons are removed but it impacts the background of the UISearchController
Can you tell me what I'm doing wrong and what should I do to integrate correctly Scope Button in UISearchController?.
I have found examples but only when the UISearchController is not embedded in the UINavigationController.
Thanks for your help!
SΓ©bastien.
You should try using the searchBar.scopeButtonTitles in your instance of UISearchController:
func initSearchController() {
let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController
searchController = UISearchController(searchResultsController: mySearchController)
// Set Scope Bar Buttons
searchController.searchBar.scopeButtonTitles = ["one", "two", "three"]
// searchController.searchBar.showsScopeBar = true //if you want it always visible
// Configure the UISearchController
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = "data.."
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
}
No need to show or hide your scopeButtons in willAppear/didDisapear. This is set by: searchController.searchBar.showsScopeBar = true

Resources