First row hidden when table view appears while search controller is active - ios

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

Related

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 to make search bar not scrolling along with table view? [duplicate]

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.

How to make search bar not disappear while scrolling in table view?

I want to make my search bar stop scrolling along with table view. I tried to add search controller into viewForHeaderInSection and it failed.Also I want to try to show original content while search bar active. So I try to do it in viewDidLoad but I don't know how to make it work. Here is my code for both of method.
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
Do you have any suggestion?
Add both the table view and the search view into another view (which becomes the view of your view controller). Then the search view will be completely disconnected from your table view.
Note that if your view controller is currently a table view controller subclass you will need to change that to be a simple view controller subclass.
You are adding the searchbar to the header view. If you want to keep it above your table you can add it to the navigation controller view. How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?

Resources