UISearchBar for UISearchController iOS 8 regain focus after resignFirstResponder - ios

I'm using the new UISearchController in iOS 8.
I have a UISearchBar which I've added to the titleView and a corresponding results controller.
I have it set up so that when the search results are scrolled scrollViewWillBeginDragging(), I call searchBar.resignFirstResponder() to hide the keyboard (same as Spotlight search, Facebook app, etc. -- let's us see the results hidden behind the keyboard).
The problem I have is that I can't seem to be able to re-gain focus on the search bar. It should be as simple as just tapping on the search bar, but this seems to end the search completely, rather than refocusing the search bar to allow the search text to be edited.
I can't figure out why the UISearchBar isn't regaining focus.
Any suggestions?

Turns out after much trial and error, the solution is surprisingly simple:
Just set dimsBackgroundDuringPresentation = false on your UISearchController and it all works fine!

Related

how to implement a searchbar above the keyboard

sorry i'm still getting into swift, and i'm building an app with a search-for-keywords feature, but i couldn't figure it out how to implement a search bar above a keyboard like the picture below:
this picture is from an app called "Developer"
i'm thinking of that adding a toolbar with searchbar inside to the keyboard might be a solution, is there any other ways to make this thing works?
sorry for my bad english....

iOS13 - UITableView + UISearchController with custom hidesSearchBarWhenScrolling behavior

On iOS 13, if you set hidesSearchBarWhenScrolling to true, the view controller will hide the search bar, and you have to scroll down to reveal it.
If you set it to false then it is immediately shown and it won't disappear.
I think this is a reasonable behavior, assuming that the end users are all versed in the way the Apple products work. Given that's not seem to be the case, I would like to show the searchbar at first visit, but then if the user starts scrolling up, the search bar is hidden. So this is a mixed behavior of the true/false.
This worked on iOS12, where I set the hidesSearchBarWhenScrolling = true in the viewDidAppear, but now on iOS 13 it's not the case. The table scrolls, but the searchbar stays on top instead of scrolling it together with the tableview(btw this is only because the screen is not rendered again).
Any idea how to go about it? I tried changing the content offset of the tableview, but no luck really.
For me it worked after adding following lines in viewDidLoad() method:
navigationController!.navigationBar.sizeToFit()
No need to specify hidesSearchBarWhenScrolling as I think by default it is true, but if you still see the search bar then you can set it true to hide it.
I faced the same issue as you and looks like such issue appears in iOS 13 only. In iOS 14 your solution works perfectly.
To make it work in iOS 13 you need to add
self.navigationItem.hidesSearchBarWhenScrolling = true
self.navigationController?.view.setNeedsLayout() // add this line
in viewDidAppear
It works for me in simulator iOS 13.7 I hope it work for you as well.

UISearchController not displaying "no results" in background and how to alter dimmed background

I have a UISearchController and the "No Results" is not showing during searches. How do I get this to appear in the UISearchController background when nothing is found? I would also like the search controller to behave like the UISearchDisplayController where the background dims when you click the searchBar initially, but undims when typing begins. I'm not sure how to do this.
For the "No Results" question, Daniel Amitay has a really good answer on another question. https://stackoverflow.com/a/4840621/4525245
For the dimming question, if I understand you right, you could just have a translucent image that adds over the UItableView after the searchBar is tapped and have it remove after the user taps the keyboard. Might not be elegant but it should work.

Attaching a custom alert view above the iOS keyboard

I'm trying to design (a "properly designed," not "hack") custom alert view. The view should attach itself to the top of the keyboard; sliding up with the keyboard (if there is an alert) or being hidden (if there is no alert).
The view should always "stick" to the keyboard... including, for instance, when the keyboard hides. In that case, the view should slide right down, out of sight, along with the keyboard.
Here's an example of what I'm trying to achieve (with an active alert):
I have originally thought about subclassing UIAlertView, but it looks like that is not recommended. And, after experimenting a bit, this is clearly a tricky task. I've got an alert that shows up but, it turns into problems staying in sync with the keyboard, and I haven't found a way to make it "track" with the motion of the keyboard... not smoothly.
Any ideas?
You can achieve this with inputAccessoryView of UITextField and UITextView. See Custom Views for Data Input chapter in Apple's "Text Programming Guide for iOS" for more information.
For example, a very simple red bar above the keyboard can be added with the following code:
let keyboardAlertView = UIView(frame:CGRectMake(0,0,320,44))
keyboardAlertView.backgroundColor = UIColor.redColor()
textField.inputAccessoryView = keyboardAlertView

Getting notified when user presses "Search" on keyboard in UISearchDisplayController

I am using a UISearchDisplayController to let the user search through a list of buildings on a university campus. Sometimes, the user will know exactly what building they want, enter the building's number, and that building will then be the only building result showing in the UITableView. At the moment, if the user proceeds to hit "Search" on the keyboard, the keyboard animates off the screen and the user then has to make a second tap on the sole item in the UITableView to be sent to a point on a map showing the location of that building.
My question is, is there a way to be notified when the user hits the "Search" button on the keyboard inside a UISearchDisplayController, so that I can perform a check to see if there's only one result, and if so, take the user straight to that result, rather than requiring them to explicitly make the second tap? I've looked at the methods provided by the UISearchDisplayDelegate, but can't see anything relevant.
UISearchDisplayController has a UISearchBar, you can set a delegate for search bar and implement -searchBarSearchButtonClicked:.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
}
This also works with the keyboard search button.

Resources