Iphone: Hide Uitableview Search Bar by Default

iPhone: Hide UITableView search bar by default

First make sure, to add the UISearchBar to the tableHeaderView of the UITableView so that it gets scrolled with the table's content and isn't fixed to the top of the view.

The searchbar isn't counted as a row in the tableview, so if you scroll the top of the tableview to the first row, it 'hides' the searchbar:

[yourTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

or in Swift:

yourTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)

Make sure to not scroll the tableview before it contains data (scrollToRowAtIndexPath will raise an exception if the given indexPath does not point to a valid row (i.e. if the tableview is empty)).

Hide UITableView search bar

To anyone else who might have this problem (which appears to be nobody) -

The only way forward I could find was to abandon the UITableViewController in favor of a UIViewController with a uiview whose children are the table view and search bar.

I manage the layout as above:

- (void)setSearchHidden:(BOOL)hidden animated:(BOOL)animated {

UISearchBar *searchBar = self.searchDisplayController.searchBar;
CGFloat searchBarHeight = searchBar.frame.size.height;

CGFloat offset = (hidden)? -searchBarHeight : searchBarHeight;
NSTimeInterval duration = (animated)? 0.3 : 0.0;

[UIView animateWithDuration:duration animations:^{
searchBar.frame = CGRectOffset(searchBar.frame, 0.0, offset);
self.tableView.frame = UIEdgeInsetsInsetRect(self.tableView.frame, UIEdgeInsetsMake(offset, 0, 0, 0));
} completion:^(BOOL finished) {if (!hidden) [searchBar becomeFirstResponder];}];
}

Except in a method that gets called when the add function starts and ends.

(About twice a year, I come to the conclusion that UITableViewController is more trouble than it's worth. Then, at approximately the same frequency, I forget I learned that).

Hide UITableView search bar by default when in a navigation controller

self.tableView.contentOffset = CGPointMake(0.0, 44.0);

The above code does in fact work but it needs to run after the UITableView has finished creating all of its cells. I guess thats another question though.

How to show/hide UISearchController searchBar in UITableView?

Figured this myself, maybe not the most optimal solution, but if anyone has better idea I would happily accept is as an answer.

-(void)searchButtonDidTap:(UIButton*)aButton
{
if (!_searchController){

_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_tableView.tableHeaderView = _searchController.searchBar;

_searchController.delegate = self;
_searchController.dimsBackgroundDuringPresentation = NO;

self.definesPresentationContext = YES;
_searchController.active = NO;

_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_tableView.tableHeaderView = _searchController.searchBar;

_searchController.searchBar.delegate = self;
}
else
{
_searchController.active = NO;
[_searchController removeFromParentViewController];
_searchController = nil;
_tableView.tableHeaderView = nil;
}

[_tableView reloadData];
}

how to make the UISearchController() hidden by default?

Found a very simple solution to hide the search bar by default, I found the answer here

I just needed to add this line in the viewDidLoad() :

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

Updated to Swift 3.0 syntax:

tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)


Related Topics



Leave a reply



Submit