Reputation: 1760
I have a UITableView
with a UISearchBar
. I need to show/hide my searchBar
, by user action (button pressing):
I tried to use this code:
if self.tableView.contentOffset.y == 0 {
self.tableView.contentOffset = CGPoint(x: 0.0, y: self.searchBar.frame.size.height)
}
from this question.
Actually I've tried all of those answers. All of them just scroll my UITableView
, and each time I'm scrolling my UITableView
- searchBar
appears.
I tried to do something like this:
self.newsTableView.tableHeaderView = nil; //Hide
and
self.newsTableView.tableHeaderView = self.SearchBar; //Show
But UITableView
doesn't want to return searchBar
;
How can I resolve this problem?
I need to hide searchBar
by action, not to scroll my UITableView
(hide like searchBar.hidden = true
) Actually, searchBar.hidden = true
works, but there is a white space instead of searchBar
.
Upvotes: 5
Views: 12683
Reputation: 2424
In Xcode 7.3 in the ViewController.swift file it worked properly for me
CategoryTableView.tableHeaderView = searchController.searchBar // show
CategoryTableView.tableHeaderView = nil // hide
searchController.active = false
Upvotes: 2
Reputation: 171
Use UIView.animation for searchBar and tableView When you start scroll table Change position/alpha of search bar and height of tableView
UIView.animateWithDuration(1.0, animations: {
searchBar.alpha = 0.0
tableView.view.frame.height = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)
}, completion: {
(value: Bool) in
//do nothing after animation
})
Upvotes: 3
Reputation: 184
hope this could solve your problem. call this at launch of tableview to hide the searchBar and when you scroll down it will appear
var newBounds: CGRect? = self.newsTableView.bounds
newBounds?.origin.y = 0
newBounds?.origin.y = newBounds!.origin.y + self.searchBar.bounds.size.height
and then set the new bound of the tableView
Upvotes: 1