UISearchBar overlaps with UITableView content when active
Asked Answered
M

5

8

I have a view controller with a table view and a UISearchController. When running the app, I found that the search bar overlaps the content when it is active. What do I need to adjust to make the content not be overlapped when search bar is active?

Normal view:

enter image description here

Search bar is active:

enter image description here

View controller settings:

enter image description here

Moia answered 2/5, 2016 at 22:58 Comment(6)
Did you do edgesForExtendedLayout = .None and automaticallyAdjustsScrollViewInsets = false?Valery
@ozgur No I didn't. I have included a screenshot of the settings.Moia
Your search bar is below the nav bar in one picture and in it in another, explain how it's presented and what it's a subview ofMcclendon
why can't you use autolayout ?Pavement
@Pavement I am using autolayoutMoia
I am still facing the same issue .....Mourant
M
5

Problem is because of you have automaticallyAdjustsScrollViewInsets = true

please uncheck that enter image description here

will help :)

Metempirics answered 5/5, 2016 at 8:12 Comment(6)
Thanks - this along with a change to layout constraint fixes it.Moia
Should Adjust Scroll View Insets usually be turned off for table view?Moia
ya even when i am doing with autolayout , i preferred to turned it offMetempirics
Thanks - will award bounty tomorrow.Moia
I am still facing the same issue ..... my section header overlaps with tableHeaderViewMourant
Try to uncheck under top bars @mihirmehtaMetempirics
C
1

You can use the 'UISearchController' in this manner:

_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;

// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others

// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
//
self.definesPresentationContext = YES;  // know where you want UISearchController to be displayed

You can use this working Apple Reference Sample code for more detail.

Cowry answered 5/5, 2016 at 7:59 Comment(1)
you should place 'SearchBar' it in the table's headerview.Cowry
D
1

Try to put your SearchBar in the header of TableView.

Drome answered 5/5, 2016 at 13:45 Comment(0)
C
1

Write this in your viewDid().

source: apple

if #available(iOS 11.0, *) {
    // For iOS 11 and later, place the search bar in the navigation bar.
    navigationItem.searchController = searchController

    // Make the search bar always visible.
    navigationItem.hidesSearchBarWhenScrolling = false
} else {
    // For iOS 10 and earlier, place the search controller's search bar in the table view's header.
    tableView.tableHeaderView = searchController.searchBar
}
Coraciiform answered 23/1, 2019 at 18:55 Comment(0)
I
0

This is related to unhiding search bar while scrolling on iOS 11.

if #available(iOS 11.0, *) {
  navigationItem.searchController = searchController
  navigationItem.hidesSearchBarWhenScrolling = false
} else {
  // Fallback on earlier versions
  tableView?.tableHeaderView = searchController.searchBar
}
Institution answered 29/7, 2018 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.