how to show the searchResultsController of UISearchController when the searchBar is pressed - ios

enter image description hereI have to type some words to make the searchResultsController show,but what I want to do is showing the searchResultsController as soon as the searchBar is active.How can I do it ?

The only way I know how to do this is by implementing the UISearchBarDelegate and then implement searchBarShouldBeginEditing.
What you can do then is something like this:
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
self.tabBarController?.selectedIndex = 1
return false
}
Unless the search view controller isn't situated in the tabbar, then you can just tell the navigationController to slide the searchResultsController into view. In the searchResultsController you can tell the searchbar to start editing with:
searchbar.becomeFirstResponder()
Just make sure to be aware from where the user comes from because I'd imagine it might be annoying if it would get called everytime viewDidAppear gets triggered.

Related

UISearchBar ends editing, hides keyboard, but screen is still dimmed

I have a view controller in which I dynamically create a UISearchController and assign it to self.navigationItem.searchController.
class MyViewController: UIViewController, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
searchController.searchBar.delegate = self
// launch the app directly into this search text box
searchController.searchBar.becomeFirstResponder()
}
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
// I tried this
searchBar.resignFirstResponder()
// and this
searchBar.endEditing(true)
// and finally this
return true
}
}
The keyboard hides away when the user touches anywhere outside the searchbar and the screen goes back to its brightness. That is the expected behavior.
However, when the user taps the [Search] button within the on-screen-keyboard, the keyboard goes away, but the screen is kept dim. None of the sub-views are useable, except if the user taps the search bar again, then the keyboard comes back.
So in short, the only way for the user to continue using the view controller, is not to hit the [search] button, which is counter-intuitive.
am I missing something?
In viewDidLoad(), add the following line:
searchController.obscuresBackgroundDuringPresentation = false
From the documentation: If you use the same view controller to display the searchable content and search results, it is recommended that you set this property to false. The default value of this property is true.

UISearchController / UINavigationBar shows broken animation when used within UINavigationController

I have this NavigationController hat has Large Titles enabled for its NavigationBar. The root ViewController has a SearchController, and hidesSearchBarWhenScrolling is set to True in the ViewController's NavigationItem as I don't want the SearchBar to be always visible. The ViewController has a TableView and when you tap on one of its items a new instance of the same ViewController will be pushed onto the Navigation stack using a storyboard segue. However, when looking at the transition between the current and the new ViewController one can observe that the animation doesn't look right: As soon as the new ViewController is moved in the SearchBar becomes empty, just showing its background. When the new ViewController is finally fully visible, the SearchBar will go away without any animation.
This is how I add the SearchController (nothing fancy here):
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
}
}
And so it looks like when navigating from "One" to "Two":
UISearchController / UINavigationBar shows broken animation when used within UINavigationController
Is there a way to make this look nicer? Of course, in the new ViewController the SearchBar should not be initially visible, so it has to go away somehow. But I would think that the SearchBar on the old ViewController perhaps should be faded out somehow instead of staying there and then suddenly hiding when the transition to the new ViewController is finished. Hopefully I'm just doing something wrong here...
Thanks and Merry Xmas to all of you,
Peter
Try setting the search controller to nil in the viewWillDissappear method.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationItem.searchController = nil
}
Well, I finally found something very useful that I just couldn't find before asking my question:
Broken UISearchBar animation embedded in NavigationItem
Too bad this is known since iOS 11 and still not fixed.

searchBarSearchButtonClicked not working IOS

I am trying to use the search bar to print out a string but the delegate function i have written does not work and I really do not understand where I am going wrong. Please can someone advise?
class LandingPage: UIViewController, UISearchBarDelegate{
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("search bar button clicked! ")
}
If you have added the search bar in storyboard then you have to ctrl+drag to ViewController the yellow button and select search bar delegate. if you have created the search bar in code then you have to assign it's delegate in your viewcontroller.swift
searchbar.delegate = self

Display nav bar when user scrolls down - swift

I have created a parallax detail view in swift. Wish to allow nav bar colour and title display when user scrolls down.
Like this example here on the detail view. This is in objective C and I can't figure out the swift version. I'm sure it's simple enough with a few lines of code in the right place.
https://github.com/KMindeguia/movies/blob/master/README.md
I know the nav bar has a .hideswhenuserswips function but can't find anything for this!
Thanks
If you use storyboard for your UINavigationController, you can set like this
Or, you can set in your code like this:
myNavigationController.hidesBarsOnSwipe = true
You can use scrollview delegate methods to show or hide navigation bar.
you can implement scrollViewDidScroll , scrollViewDidEndDecelerating or scrollViewWillBeginDecelerating.
from this delegate methods you can manage your navigation bar.
This component just using simple UIView and implementing UIScrollViewDelegate methods. You can add your custom view in top of parent view and hide it, implement UIScrollViewDelegate methods and track some contentOffset of uiscrollview. Like in this component from lines 237
scrollDelegate methods
Set the NavigationBar in each viewcontroller, if you would not show navigationbar use this code,
self.navigationController?.navigationBarHidden = true
And show the navigationbar in particular viewController put this below code,
self.navigationController?.navigationBarHidden = false
this lines used your method, or you use this code,
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = true
}
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
self.navigationController?.navigationBarHidden = false
}
when scrolling to show your navigationbar see this link Hide status bar while scrolling
hope its helpful

How to hide the transition searchBar next screen

Added The Search bar TableView, but when one of the items that were looking for and the transition to his page SearchBar and module typing is not removed and remains on the screen. How can we make the transition to searchbar module typing cleaned?
If Have you have used UISearchController,put viewWillDisappear: in viewController.
//this is my searchController, created programatically
var searchController : UISearchController?;
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated);
if let sc = self.searchController where sc.active {
self.searchController?.dismissViewControllerAnimated(false, completion: nil);
}
}
Reason :
UISearchController is subclass of UIViewController.
If your search bar is active, that means it over your current viewController. so before going out from your viewController you need to check if it is active or not, by using its property .active.

Resources