iOS Fix Search Bar on Top of the Uitableviewcontroller

iOS Fix search bar on top of the UITableViewController?

The reason why your searchbar is scrolling with the table contents is that you have put it directly IN the table, thus making it a child header section of the table. and that section ALWAYS scrolls…

Here is how this this can be achieved. And it is actually quite simple. (The following example relies on Storyboard, but the mechanism is the same whatever you are using) :

1) Use a UIVIewController and NOT a UITableViewController

2) Add a UITableView as the child of the parent UIView

3) Add a UISearchBarController also as a child view of the UIView, NOT as a child of the UITableView (UITableView and UISearchController are siblings)

you should have the following layout :

Sample Image

EDIT : The important thing to remember is to put the UISearchBarController ABOVE the sibling UITableView. Otherwise you may see the UITableView overlap the UISearchBarController when the latter is focused.

EDIT 2 : BTW, if you are using AutoLayout, remember to set the TOP constraint of the tableView relative to the SearchBar…

Run it and admire the result.

Hope this helps.

iOS 11 search bar jumping to top of screen

It is encouraged to apply the new way to show search bar/search controller on iOS 11.
Here is what I have done:

    if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}

cannot see searchbar when placed at top of UITableViewController

Just drag n drop search bar at top of table view controller (just above to prototypecell)
Sample Image

Sample Image

Floating/fixed search bar above TableView - Swift 3

I actually discovered the answer. The issue was simply that I had the line:

tableView.tableHeaderView = searchController.searchBar

I removed this, and then the search bar disappeared, but it was only being hidden behind the navigation bar! So I played around with the constraints on the interface builder and it now works as normal :)

How to stick top of the UITableView to the bottom of the UISearchBar?

Option A (search bar in navigation bar):

let tableView = UITableView()
view.addSubview(tableView)

let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController

definesPresentationContext = true

tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
])

Option B (search bar as table header view):

let tableView = UITableView()
view.addSubview(tableView)

let searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar

definesPresentationContext = true

tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
])

Option A
Option B

How to fix table view's search bar overlapping with status bar

I think you need this code.

In your viewDidLoad method add this code:

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

And your tableview will be something like this:

Sample Image

EDIT:

You can forcely scroll table with this code:

tableView.scrollToRowAtIndexPath( NSIndexPath(index: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)

iOS 11 search bar in tableViewHeader jumps to the top of the screen on focus

Since iOS11, search control is part of navigation bar. You will need something like the following

Objective-C

if (@available(iOS 11.0, *)) {
self.navigationItem.searchController = searchController
} else {
self.tableView.tableHeaderView = searchController.searchBar
}

Swift

if #available(iOS 11.0, *) {

UITableView jumps to top showing SearchBar when Hide Bottom Bar on Push is true for segue from Cell

Well, I couln't find out how to fix this but at least I found a workaround to stop the jumping. I observed the same issue in other apps and even apple doesn't hide the search bar on app start anymore. Maybe because of this issue, who knows? So here is my solution.

First, this issue only happens if your table view has less cells than it would need to fill the whole screen. As soon as you have more cells than can fit on screen, after going to the detail view and then back to the master view, the tableview does not jump and stays at the very same position.

With this knowledge, I was able to create a workaround. My workaround is rather simple. I use segues to show various parts of my app so in my UITableViewController which is the "master", in prepare(for segue:sender:), before I show the detail view, I check if the first cell of the table view is visible. If so, I scroll the tableview to show the search bar.

if let firstCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) {
if tableView.visibleCells.contains(firstCell) && !searchController.isActive {
let offset = CGPoint(x: 0, y: -tableView.contentInset.top)
tableView.setContentOffset(offset, animated: true)
}
}

When clicking one of the cells, a segue to show the details of the entry is performed. If the first cell is visible, i scroll the tableview down a little bit to show the search bar.

A better approach might be to actually calculate how many cells can fit on the screen and how many cells we have in total so we really only show the search bar when there are not enough cells but I found that this solution is good enough for me since it prevents jumping.



Related Topics



Leave a reply



Submit