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
Related
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
I'm using UISearchController with a UITableViewController. When I have a search in progress, and I select a row in the table to go to the detail view, and then go back to the table view, the first row of the table is hidden behind the search bar.
I can scroll the first row back into view, but then it just pops back behind the search bar when I let go of the scroll. So frustrating!
In viewDidLoad I have:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
mainTable.tableHeaderView = searchController.searchBar
Removing definesPresentationContext = true solves the problem, but I need that to prevent the search bar from showing up if the user segues to other view controllers.
I've spent hours searching for a solution and trying different ways to fix the problem.
Is there any way to set the first row of the table so it is below the search bar when the table view re-appears?
I tried setting definesPresentationContext = true in prepareForSegue, but that did not work.
I found a way to solve this problem.
Do not set definesPresentationContext = true
In prepare(for:sender:), add the following. This keeps the search active, but prevents the search bar and keyboard from appearing on the view controller being segued to.
searchController.searchBar.isHidden = true
searchController.searchBar.endEditing(true)
In viewWillAppear, add the following. This makes the search bar visible when the view controller with the search controller comes to the top of the navigation stack. The first row of the table is positioned properly below the search bar.
searchController.searchBar.isHidden = false
In willMove(toParentViewController:), add the following. This cancels any active search, preventing the search bar from appearing on the view controller coming to the top of the navigation stack.
if parent == nil {
searchController.isActive = false
}
This is working for me:
self.extendedLayoutIncludesOpaqueBars = true
This question already has an answer here:
iOS7: How to add a fixed Search Bar like Contacts on iOS7?
(1 answer)
Closed 7 years ago.
I want to make a search bar like this.
The search bar in contacts app is stay while I scroll down. I want to make a search bar with this property that It doesn't scroll down along with table view . I did my app in table view controller.This is my code in my table view controller.
myTableViewController(in my viewDidLoad function):
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
It should work like search bar in contact because of this code
tableView.tableHeaderView = searchController.searchBar
Is there something i do wrong? If not then how to make my search bar doesn't scroll down with table view(in table view controller)?
Instead of a search bar in a table view, make a search bar and a table view. Thus, the search bar just sits there while the table view scrolls.
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
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.