IOS8 UISearchController display searchbar directly - ios

I am trying to display a simple search screen for my iphone app using a UISearchController.
I used a UITableViewController and a UISearchController that I embedded inside a UINavigationController
This get me the following screen (state 1)
When user click on the search field I get the screen below (state 2):
The search bar display nicely at the top of the screen below the status bar.
What I want to do is to get state 2 straight away without going to state 1
I tried to add this code
presentViewController(searchController, animated: true, completion: nil)
but it either does not show the searchbar if animated is false or display it as I want but after an unnecessary transition that temporaryly shows the navigation bar

Ok I just found how to do it reading this answer UISearchController in a UIViewController
I just need to add the searchbar like this to get the desired effect
searchController.hidesNavigationBarDuringPresentation = false
// tableView.tableHeaderView = searchController.searchBar
navigationItem.titleView = searchController.searchBar

Related

Swift: weird "snapping" search bar animation after cancel button selection using UISearchController?

I have a weird issue, where upon searching SO, could not find anyone else experiencing this issue.
Basically I have a UITableView inside a UIViewController, which is embedded in a UINavigationController. I've set up a UISearchController above the UITableView with the appropriate UISearchResultsUpdating and UISearchBarDelegate functionality simply like so:
var sC = UISearchController(searchResultsController: nil)
...
sC.dimsBackgroundDuringPresentation = false
sC.searchBar.placeholder = "Searching..."
sC.searchBar.delegate = self
sC.searchResultsUpdater = self
table.tableHeaderView = sC.searchBar
The problem is that when the search bar is visible, and the Cancel button is initiated, the search bar will animate back in place, but right before it stops, it has this snapping animation.
This can be seen in the demo video link (animated gif doesn't clearly show the issue): Search Bar Issue
If it is not expected behavior, how can I fix this so that the animation of the search bar is much more fluid instead of this snapping animation?
Thanks!

iOS 11 tableView with searchBar issue

Please help to determine the reason of such behavior (it's hard to describe by words, so I recorded the short video) https://youtu.be/E2ks0liFX4I
In short words:
Initially it's able to scroll content beneath navigation bar. If press search field - the search bar looks like detached from table view and goes too high and overlapped by status bar (look at increased space between grey border of search bar and first row in the table). After Cancel button pressed - the search bar jumps down and now can't be hided by scrolling.
I'm using UITableViewController.
Search bar initialization in viewDidLoad:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
And in StoryBoard:
tableView settings
Not sure what u want. I guess u want the search bar to hide when scrolling?
I set searchbar to the navigationItem, then set the hidesSearchBarWhenScrolling property.
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true

Swift ios11 NavigationItem SearchBar won't hide completely

Having an issue with adding a search controller to a navigationItem in iOS11 / Swift 4.
Basically everything works as expected for the most part, pull down will reveal the search bar and searching works fine. However when trying to hide the search bar by scroll back up... the bar won't hide completely and remains as a thin strip (see below).
I have declared my search controller as follows:
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.scopeButtonTitles = ["All", "Samples"]
searchController.searchBar.delegate = self
navigationItem.searchController = searchController
Before (scroll down to reveal search bar):
After (scroll up to hide):
As discussed in this Apple forums thread.
When the value of this hidesSearchBarWhenScrolling is true, the search bar is visible only when the scroll position equals the top of your content view, that's in case you are using a UIViewController, which view property is a normal UIView (It is not a subclass of UIScrollView).
Instead, try to use UITableViewController or ScrollView, it should work as expected
In my case it was happening only when there was a small num of items in the table.
I came to a non-technical, but rather a logical solution to show the search bar only when there are >10 items in the list. There is no need to have search when you have only a few items anyway.
Try to add this
self.navigationItem.hidesSearchBarWhenScrolling = true

self.definesPresentationContext = true: leads to black screen?

let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
// searchController.definesPresentationContext = true
self.definesPresentationContext = true
When the search bar is active, with text in it, and I go to another tab and then back, the controller is black, apart from the actual search bar. Things go back to normal when I cancel and empty the search text field.
My question is basically identical to this question: UISearchController causes black screen Swift 2.0
Except that that answer does not solve my problem as you can see. What does solve it is if I change self to searchController, thus: searchController.definesPresentationContext = true. But this results in the search bar appearing in the next controller I tab to. Very confused, please help.
I am using a tableview embedded in a navigation controller and tab bar controller. viewDidDisappear is not being called when the search is active.
Not a solution per se, but a workaround, this stackeroverflow post helped:
TableView with SearchController - DEINIT not called. I am not sure if this is some kind of apple bug.
Apparently I am not supposed to use self.definesPresentationContext = true at all. This makes my search appear in all my tabs. But at least viewDidDisappear is called.
In viewDidDisappear, I can hide the search bar with searchController.searchBar.hidden = true and show the bar again in viewDidAppear.
in the AppDelegate.swift
window?.backgroundColor = UIColor.white

How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?

I'm trying to use the new UISearchController from iOS 8, and embed its UISearchBar in my UINavigationBar. That's easily done as follows:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = searchController.searchBar
But when I add the scope buttons:
searchController.searchBar.showsScopeBar = true
searchController.searchBar.scopeButtonTitles = ["Posts, Users, Subreddits"]
It adds the buttons behind the UISearchBar and obviously looks very odd.
How should I be doing this?
You're bumping into a "design issue" where the scopeBar is expected to be hidden when the searchController is not active.
The scope bar buttons appear behind (underneath) the search bar since that's their location when the search bar becomes active and animates itself up into the navigation bar.
When the search is not active, a visible scope bar would take up space on the screen, distract from the content, and confuse the user (since the scope buttons have no results to filter).
Since your searchBar is already located in the titleView, the (navigation and search) bar animation that reveals the scope bar doesn't occur.
The easiest option is to locate the search bar below the
navigation bar, and let the searchBar animate up into the title
area when activated. The navigation bar will animate its height,
making room to include the scope bar that was hidden. This will all
be handled by the search controller.
The second option, almost as easy, is to use a Search bar button
icon, which will animate the searchBar and scopeBar down into
view over the navigation bar.
- (IBAction)searchButtonClicked:(UIBarButtonItem *)__unused sender {
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
self.searchController.searchBar.scopeButtonTitles = #[#"Posts", #"Users", #"Subreddits"];
[self presentViewController:self.searchController animated:YES completion:nil];
}
If you want the searchBar to remain in the titleView, an animation
to do what you want is not built in. You'll have to roll your own
code to handle the navigationBar height change and display your own
scope bar (or hook into the internals, and animate the built-in
scopeBar down and into view).
If you're fortunate, someone else has written willPresentSearchController: code to handle the transition you want.
If you want to always see a searchBar and scopeBar, you'll probably have to ditch using the built-in scopeBar, and replace it with a UISegmentedControl which the user will always see, even when the search controller is not active.
Update:
This answer suggested subclassing UISearchController to change its searchBar's height.

Resources