How to disable or hide the scope buttons in searchbar? - ios

I am new to ios I am using Xcode 7 and swift 2 I have implemented a searchbar controler with my tableViewController like this:
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
definesPresentationContext = false
searchController.dimsBackgroundDuringPresentation = false
// Setup the Scope Bar
searchController.searchBar.scopeButtonTitles = ["All", "Chocolate"]
searchController.searchBar.showsScopeBar = false
searchController.searchBar.searchBarStyle = .Default
tableView.tableHeaderView = searchController.searchBar
But I don't want the scope buttons in this case. How can I hide them?
Note, this is not working for me:
searchController.searchBar.showsScopeBar = false

Storyboard
Using Search Display Controller via storyboard, go to the search bar attributes inspector and uncheck the "Shows Scope Bar" if it is checked.and remove the scope titles if any.
Programmatically
Using Search Display Controller programmatically then you have to remove this part, will not show the scope bar
searchController.searchBar.scopeButtonTitles = ["All", "Chocolate"]
searchController.searchBar.showsScopeBar = false
Or
Make the scopeButtonTitles to nil will also remove the buttons in scope bar
searchController.searchBar.scopeButtonTitles = nil

Related

Search Bar disappears in scrolling, how to prevent this action

My search bar seems to disappear on scrolling, even if i were use
navigationController?.hidesBarsOnSwipe = false
Also using below
navigationItem.searchController = searchController
has no effect
Seems to have no effect, my code for search controller addition is
searchControlInstance = UISearchController(searchResultsController: nil)
searchControlInstance.searchResultsUpdater = self
searchControlInstance.obscuresBackgroundDuringPresentation = false
self.definesPresentationContext = true
searchControlInstance.searchBar.returnKeyType = .done
navigationItem.hidesSearchBarWhenScrolling = false
I do have
navigationController?.navigationBar.prefersLargeTitles = true
but turning it to false seems to have no effect the search bar just disappears like in picture, where am i making the mistake, can any one suggest, i do not use storyboard
You have to set it in this way:
navigationItem.searchController = searchControlInstance
navigationItem.hidesSearchBarWhenScrolling = false

What is this strange white space at the top of my UIViewController when using the Search Controller on my device?

Left side displays my app in the iPhone 7 simulator. Right side is the app as displayed in my device. When tapping in the search bar, the navbar sildes up but then slides back down as showing in the image with the "What is this?" text.
The code for my search controller is as follows and it is being called in viewDidLoad.
//MARK: - Search Controller
func configureSearchController() {
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
// to receive updates
searchController.searchResultsUpdater = self
// Set up searchbar of search controller
searchController.searchBar.scopeButtonTitles = ["All", "Something", "Something"]
searchController.searchBar.delegate = self
// add the searchbar to the tableview
tableView.tableHeaderView = searchController.searchBar
// hack to fix ocassional bugs
searchController.searchBar.sizeToFit()
}

How to stop search bar from hiding on scroll

I have a UINavigationController that has a UISearchController assigned to the navigationItem's searchController. Inside the view I have a UICollectionView. The search bar displays in the navigation bar, but when I scroll, the navigation bar automatically hides. Can I stop this from happening?
let searchController = UISearchController(searchResultsController: recentSearchesViewController)
searchController.searchResultsUpdater = recentSearchesViewController
searchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
navigationItem.searchController = searchController
You have to set :
navigationItem.hidesSearchBarWhenScrolling = false

Search Bar disappear when typing

I have a problem with my search bar. As I begin to type, my search bar disappears. I've tried many solutions that have been listed here but none of them worked. I've added the search bar right under my nav bar and on top of my custom collection view, however I did not add it to the header of my collection view nor did I add it to the nav bar. I also want to note that I've done most of my code programmatically.
Here are some of the methods that I have already tried:
func setupSearchController(){
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
self.definesPresentationContext = false
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.placeholder = "Type Here..."
searchController.searchBar.sizeToFit()
// let text = searchController.searchBar.text
searchController.isActive = false
// searchController.searchBar.text = text
self.searchController.extendedLayoutIncludesOpaqueBars = true
searchController.searchBar.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
}
Before picture
After typing picture

Add UISearchBar to UINavigationBar with UIBarButtonItem

In my app, I used to have a search bar in the header view of my table view. However, I have added a search bar button item, and when a user taps it, I want a search bar to animate across the navigation bar, and the user should be able to search. This is kind of like the search bar in the Twitter iOS app. here is the code for my UISearchController:
self.searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = true
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
//self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
controller.searchBar.returnKeyType = .Search
controller.searchBar.delegate = self
return controller
})()
Here is how my UINavigationBar looks like right now:
How would I go about doing this?
You're almost there. You need to do four things:
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = searchController.searchBar
hide all the current UIBarButtonItems you have in the navigation bar
searchController.searchBar.becomesFirstResponder()
That is, you setup your UISearchController so it doesn't hide the navigation bar, as the UISearchBar is gonna be displayed there.
Then, when your IBAction runs, you do something like:
navigationItem.hidesBackButton = true
navigationItem.titleView = searchController.searchBar
navigationItem.rightBarButtonItem = nil
searchController.searchBar.becomeFirstResponder()
That guarantees the UISearchBar will use the entire UINavigationBar when it becomes the first responder. You also get the UISearchBar animation, which does a good job in hiding the unanimated removal of the UIBarButtonItems.
You will need to implement the UISearchBarDelegate (or if you prefer UISearchControllerDelegate) to restore the original navigation bar state once the user is done with the search.
One very important detail is that in order for your UISearchBar to be editable in the titleView, no other view controller can be set with definesPresentationContext = true at the time yours is pushed.

Resources