Empty space at the bottom of UITableView after adding UISearchBar - ios

I'm making a UITableView that displays a record of past events. It all looked great until I added a search bar at the top. It all looks how I want when it's scrolled to the top:
However, when I scroll to the bottom there's now a bunch of white space. It's hard to tell in this screenshot as there's no screen border but the tableview scroll will rest on this screen with about half the page blank:
Interestingly, when I activate the search bar at all (i.e. click in it and then press cancel) the white space goes away until this view appears again.
I've tried all sorts of settings for the tableView as well as using reloadData() at various stages of the view layout process to no avail. I know the number of rows is correct. The only thing I've found to make the white space go away is to disable the search bar. But I want the search bar!
Here's the code for the search bar:
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.placeholder = "Search by keyword, name, etc."
controller.searchBar.barTintColor = UIColor(red: 224/256, green: 244/256, blue: 255/256, alpha: 1)
controller.searchBar.delegate = self
self.pastServicesTable.tableHeaderView = controller.searchBar
return controller
})()

Same thing happened to me. Stating the the tableview row height in the viewDidLoad (ie. self.tableView.rowHeight = 350) fixed it.

I solved extra bottom space this way (in viewDidLoad):
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 200 // should be more than real row height will be

It seems like you are observing UIKeyboardWillShowNotification & UIKeyboardWillHideNotification and changing the contentInset of the tableView (adding keyBoard size height to contentInsets) and not setting the contentInsets to UIEdgeInsetsZerowhen keyboard hides.

Related

Swift: Hide Scrollview header

I would like to hide the header of my scrollview.
I have a scrollview
//Contains everything inside the view
let scrollView = UIScrollView()
scrollView.alwaysBounceVertical = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.showsHorizontalScrollIndicator = false
which spans across the whole width and height of my view. When scrolling down, a weird header appears and blocks a bit of the scroll view content. I would like to hide that overlay. The worst thing is, that in bright mode, the overlay is white. That clashes with my colors. How do I go about removing it?
The overlay is the navigation bar - it appears as soon as you scroll. To hide it completely, you can set navigationController?.setNavigationBarHidden(true, animated: false). Or, you can pass in a transparent appearance for navigationController?.navigationBar.standardAppearance.

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

Custom position for UISearchBar

I'm currently trying to implement UISearchController and its associated UISearchBar.
I aim to have a UISearchBar just above my UITableView which goes top as soon as it has the focus, like the picture below (screenshot from my old UI with UISearchDisplayController, now deprecated):
I already tried to set my UISearchBar as header of the UITableView, it works well but I have 2 issues :
The search bar logically scrolls with the table view content
When the search bar takes the focus, it remains at the same position
Here is the way I configured my UISearchBar:
func configureSearchController() { // Method called in the viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController!.searchResultsUpdater = self
searchController!.dimsBackgroundDuringPresentation = false
searchController!.searchBar.placeholder = "searchfood.searchbar.placeholder".localized
searchController!.searchBar.sizeToFit()
searchController!.hidesNavigationBarDuringPresentation = false
print(searchController!.searchBar.frame)
// Place the search bar view to the tableview headerview.
foodsTableView.tableHeaderView = searchController!.searchBar
// Search bar
searchController!.searchBar.searchBarStyle = .prominent
searchController!.searchBar.barTintColor = ColorLSDP.corail
searchController!.searchBar.tintColor = ColorLSDP.beige
}
So I tried to remove the following lines:
foodsTableView.tableHeaderView = searchController!.searchBar
But when I did this, I was not able to see the UISearchBar anymore.
After checking some other SO topics, I just spotted that my UISearchBar was maybe hidden below my UINavigationBar. So I tried to add the following lines in the viewDidLoad() method:
self.navigationController?.navigationBar.isTranslucent = false
self.edgesForExtendedLayout = []
It changed nothing.
Anyway, it did not solve my main issue which is having this search bar initially positioned like the screenshot above.
Is there a way to do this properly with the brand new UISearchController ?
Thanks a lot for your upcoming answers,
Regards,
Seb R.

Tapping in the UISearchBar causing table contents to shift down, and periodically the searchbar itself will move

My search is working perfectly, however when I first tap into the searchbar the cells from the tableview it is attached to will visibly shift down revealing the 'pull to refresh'. I am using a separate search controller as the search is reused in other views.
self.searchController = UISearchController(searchResultsController: self.resultsController)
self.searchController.searchResultsUpdater = self.resultsController
self.searchController.searchBar.placeholder = "Type symbol or company ..."
self.searchController.searchBar.barTintColor = UIColor(red: 12, green: 26, blue: 29, alpha: 1.0)
self.searchController.searchBar.barStyle = UIBarStyle.Black
self.searchController.hidesNavigationBarDuringPresentation = false
self.resultsController.searchBar = self.searchController.searchBar
tableView.tableHeaderView = self.searchController.searchBar
Note self.resultsController is a custom UITableViewController with appropriate methods and an extension for the results updater.
Can anyone tell me what would cause this sort of behaviour and how I can correct it?
Here are some screens to help illustrate the issue:
The interesting thing is that if I navigate to another view, or refresh the initial tableview. The layout corrects itself, and the search behaves exactly as intended.
I tried numerous solutions, but noting the behaviour above. Once you activated search, then navigated to another view. Any subsequent access of the search it was positioned correctly.
I moved my search code into the viewWillAppear method in my table view controller and vola. It works perfectly.
I suspect this has to do with the fact that my layout is
TabController -> NavigationController -> TableviewController
Put the following line in viewDidLoad solved the problem for me:
self.definesPresentationContext = false

SearchBar in TableViewController

In my app I have a UITableViewController, and I want to add a searchbar to it. The tableview has 3 sections which expand when clicked on one of them. I added this code to viewDidLoad:
searchBar.showsScopeBar = true
searchBar.delegate = self
searchBar.frame = CGRectMake(0, 0, tableView.frame.size.width, 100)
self.view.addSubview(searchBar)
The problem is: the searchbar appears in the first section when you open it, and it should appear above the whole tableview, any suggestions?
Any help is appreciated!
You can add searchBar as a tableHeaderView of the TableView of your UITableViewController.
You can find pretty helpful example in Programming iOS 9 by Matt Neuburg, inside chapter II: Table Views and Controller Views.
let src = SearchResultsController(data: self.sectionData)
let searcher = UISearchController(searchResultsController: src)
self.searcher = searcher
searcher.searchResultsUpdater = src
let b = searcher.searchBar
b.sizeToFit() // crucial, trust me on this one
b.autocapitalizationType = .None
self.tableView.tableHeaderView = b
self.tableView.reloadData()
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition:.Top, animated:false)
He also warns, that adding search bar as the table view's header view may have an odd effect: it causes the table view's background color to be covered by an gray color, visible above the search bar when the user scrolls down. The official workaround is to assign the table view a backgroundView with the desired color.

Resources