How to set SearchBar text field text - ios

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.

Related

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 can I mimic safari's search bar UI/animations in Swift?

I am attempting to mimic the search bar behavior below in iOS Safari on my current app:
I mainly want the navigation bar to scroll up to a very small version when the user scrolls on the content and comes back when they scroll back up.
I've tried using scrollViewDidScroll but I cannot seem to get it to mimic that behavior. I'm unsure if I am adding the search bar correctly to the navigation bar.
let searchBar: UISearchBar = {
let sb = UISearchBar()
sb.autocapitalizationType = .none
sb.autocorrectionType = .no
sb.keyboardAppearance = UIKeyboardAppearance.default
sb.placeholder = "Search"
return sb
}()
fileprivate func setupNav() {
//Basic Setup
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
//Search Bar
searchBar.sizeToFit()
navigationItem.titleView = searchBar
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//Implement
}
let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchResultsUpdater = self
navigationItem.searchController = searchController
definesPresentationContext = true
Put your SearchController in the Navigation Item instead. This will add collapsable SearchBar. Additionally you can try with Navigation Item Prompt for very small title on top.

Can't focus searchBar in searchController in UINavigationBar

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...

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

searchResultsController with a UITableViewController hides the navigation bar, how to prevent it?

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**
}

Resources