Search Bar always visible - swift

Hello I want the searchBar always visible, that is what I have:
searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
Is there way to accomplish this?
Thanks in advance

What about
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchController.searchBar.becomeFirstResponder()
}
?
Have a look at Apple's example (source code): https://developer.apple.com/library/content/samplecode/TableSearch_UISearchController/Introduction/Intro.html
NB - from iOS 11 onwards you should use UINavigationController instead of the table header view:
if #available(iOS 11.0, *) {
// For iOS 11 and later, we place the search bar in the navigation bar.
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.searchController = searchController
// We want the search bar visible all the time.
navigationItem.hidesSearchBarWhenScrolling = false
} else {
// For iOS 10 and earlier, we place the search bar in the table view's header.
tableView.tableHeaderView = searchController.searchBar
}

In viewDidLoad where you have your UISearchController setup:
navigationItem.hidesSearchBarWhenScrolling = false

You could try to put it in the header for the first section like this:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return self.searchController.searchBar
}
I had to solve a very similar situation, and this is what I ended up with:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
searchController.searchBar.tintColor = CARBONCOLOR
searchController.hidesNavigationBarDuringPresentation = false
self.definesPresentationContext = true
let search = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "presentSearch")
self.navigationItem.setRightBarButtonItem(search, animated: true)
With this code to present the searchController
func presentSearch()
{
self.navigationController!.presentViewController(searchController, animated: true, completion: nil)
}
This would put a barbutton in the navbar which would present the searchController over the navbar. This is not exactly what I wanted, but turned out to be the simplest way to allow search from any point in the tableview. It also saves a bit of space, which is better. If you don't have a navbar, you can present from self.

The UISearchController class is supposed to be hidden when not in use (i.e. when no search is performed), so I would suggest to either use it in a way it was designed for or go another way.
In your case it might be more appropriate to implement a UISearchBar and e.g. configure it as an item within an (existing) UINavigationBar. That way, the search bar will be always visible.
Do you already have a view controller which holds a navigation bar in your view hierarchy? If so, try to set the search bar as an item and implement display of search results in another way as intended with the UISearchController pattern.

You're setting your searchBar in the tableHeaderView of your UITableView, the default behaviour is that the searchBar scroll with the UITableView, I'm afraid that you cannot change this in the way you did it.
But you can change the way you did it, you can use the UISearchDisplayController and manage the view yourself adn put it above your UITableViewin your hierarchy view, these two tutorials can help you a lot about how to achieve it and understand how it works:
Tableview Search in Swift
UISearchController Tutorial: Getting Started
I hope this help you.

Related

Need to show navigation title while using searchBar in navigationBar

I need to show large navigation title while search with searchBar, but when I'm clicking on Searchbar or starting to type my NavigationTitle is replaced with SearchBar. I checked methods for navigationItem and didn't find any suitable.
Does anybody know how can we always show title, even while searching?
Here is my code
class FavouriteVC: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Favourite"
navigationItem.searchController = searchController
navigationItem.largeTitleDisplayMode = .always
}
}
And here are shots
screen with NavTitle
screen without NavTitle
Seems like UISearchController has a hidesNavigationBarDuringPresentation that prevents the navigation bar from hiding when set to false
Include:
searchController.hidesNavigationBarDuringPresentation = false
in your viewDidLoad and if I've understood your issue right, this should do what you need.

How to let search bar disappear immediately when moving to another VC

Below Gif is from my app, the 1st VC includes a search bar to filter the songs, and when press a row to transition to 2nd VC to show selected playing song.
The question here is that when 2nd VC is opened, the search bar is not disappeared immediately, it has like 1 or 2 seconds delay, could see that behavior from below GIF.
/ / / Here is my code, how could I solve this issue? Any hint is appreciate.
The search bar
var resultSearchController = UISearchController()
override func viewDidLoad() {
...
// add search bar
resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
// set search Bar covered color, same with tableView's background color.
controller.searchBar.barTintColor = UIColor(rgb: 0x292f33)
self.tableView.tableHeaderView = controller.searchBar
return controller
})() // closure, learn it later!!
...
}
I set search bar to disabled state when leaving current VC.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
resultSearchController.isActive = false
}
/ / / Update, as matt's comment, I change the code to integrate search bar into navigation bar, and now the search bar is disappear immediately after opening VC2.
remove self.tableView.tableHeaderView = controller.searchBar and integrate the search bar into the nav bar navigationItem.searchController = resultSearchController. Now the behavior is same as Apple's inbox app.
searchController.searchBar.isHidden = false
Hiding the searchController instead of making the active state to false may solve your issue.
Try adding in the above line to your code in viewWillDisappear()
hopefully this helps.

Why my Searchbar stay at the top only if I use it?

I have a tableView containing a list. I use a searchbar to filter the data. When I touch chararcter in the search text, I can display filtered datas and the searchbar stay at the top when I scroll. But, if I don't use it (searchbar is empty), when I scroll, the searchbar is the first line of the list and disappear when I scroll down. You can find a part of code there :
class Liste*****Active: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating,
UISearchBarDelegate,RecupListe*****ModelProtocol {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
ListeTab*****.tableHeaderView = searchController.searchBar
self.ListeTab*****.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func updateSearchResults(for searchController: UISearchController) {
// If we haven't typed anything into the search bar then do not filter the results
if searchController.searchBar.text! == "" {
filtered*****s = feedItems
} else {
// Filter the results
filtered*****s = feedItems.filter { ($0 as! *****Model).nom*****!.lowercased().contains(searchController.searchBar.text!.lowercased()) } as! [*****Model] as NSArray
}
self.ListeTab*****.reloadData()
}
Nothing is defined in storyboard.
Did I forgot part of code ?
Thanks
Given your search bar is embedded in navigation bar, you need to override this behavior:
navigationItem.hidesSearchBarWhenScrolling = false
Update
You use searcher as table header.
ListeTab*****.tableHeaderView = searchController.searchBar
So that's basically an issue -- your search bar is part of the table content that's why it's getting scrolled. To avoid that you need to get your search bar out of the tableView.
Possible solutions:
Embedding searchBar into the navigation controller header view.
Adding a searchBar as a separate view on top of the table view in the view hierarchy

UISearchController shifted up when typing under UINavigationController

I embedded a viewcontroller(BlueViewController) in a navigation controller.
the blueViewController has a UISearchController that set as the blueViewController's tableView headerView.
the searchController works fine except when it's active, it shift up and hide behind the UINavigationController(or somewhere)
I've tried to add the search controller's searchBar as a subview to the viewController, or positions a UIView and assigned the searchBar to that UIView, none of these works
I have tried this link, doesn't work
Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar
here is the searchController in BlueViewController
let searchController:UISearchController = {
let controller = UISearchController(searchResultsController: nil)
controller.hidesNavigationBarDuringPresentation = false
controller.obscuresBackgroundDuringPresentation = false
controller.searchBar.inputAssistantItem.leadingBarButtonGroups = []
controller.searchBar.inputAssistantItem.trailingBarButtonGroups = []
return controller
}()
In viewDidLoad
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
definesPresentationContext = true
you can also find entire project here
https://github.com/QiquanLu/TestNavigationWithSearchController
Any hint would be appreciated, thanks!
For iOS 11 and above, you should be setting the searchController property on BlueViewController's navigationItem like so:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
}
Don't add it as the tableView's header.

UINavigationController transition issue

This is a Apple simple guide about Table Search with UISearchController, and I have downloaded the demo project and it is working well.
https://developer.apple.com/library/content/samplecode/TableSearch_UISearchController/Introduction/Intro.html
I want to have transparent navigation bar on product detail view (DetailViewController.swift). After changing some behaviors I have transition issue when going back from product detail to device list with swipe gesture(Video link below).
https://www.dropbox.com/s/denqomhhxasxv58/issue.mov?dl=0
For transparent navigation bar I have added this lines on DetailViewController
viewWillAppear method
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// ...
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
}
In MainTableViewController.swift I have added this code to make navigationBar default style.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
}
Also I made changes on MainTableViewController viewDidLoad method and sets this properties to true.
navigationItem.hidesSearchBarWhenScrolling = true
searchController.dimsBackgroundDuringPresentation = true
So my problem is to fix that issue. Anyone can help me?
Update:
Swipe transition from product detail to device list
Search controller active state when product detail not opened
Search controller active state after product detail opened