I have UISearchController in my app:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
if #available(iOS 9.1, *) {
searchController.obscuresBackgroundDuringPresentation = false
}
searchController.searchBar.frame = searchBarView.frame
searchBarView.addSubview(searchController.searchBar)
searchController.searchBar.delegate = self
The problem is that when I enter the SearchBar and then click the cancel button the search bar not back to the frame I define here(and stretched to full size screen):
searchController.searchBar.frame = searchBarView.frame
Any idea what can be the problem?
Related
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
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.
I have TableViewController: UITableViewController.
I have implemented
let searchController = UISearchController(searchResultsController: nil).
I use SearchBar as tableView.tableHeaderView
While I'm using search, SearchBar moves up/down when I click cancel button. Why does it happen?
searchController.searchBar.delegate = self
definesPresentationContext = true
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.returnKeyType = .done
searchController.searchBar.enablesReturnKeyAutomatically = false
check it
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.
I am pushing a viewController where I want a searchBar, but search bar is not showing at all. Below is the code. Am I missing something?
var searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
definesPresentationContext = true
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
} else {
// Fallback on earlier versions
navigationItem.titleView = searchController.searchBar
navigationItem.titleView?.layoutSubviews()
}
You need to add this line to your code:
navigationItem.hidesSearchBarWhenScrolling = false
That removes hiding searchBar while scrolling and shows it on pushing your view controller.
So, navigationItem.hidesSearchWhenScrolling only works when you set the searchController property of navigationItem NOT when you set the navigationItem.titleView to searchBar.