Reputation: 4778
In my app I have a search bar in the header of myUITableView
. I tried to set the content offset to my UITableView
for hiding the search bar but it is giving me some problems.
Finally I solved like this:
- (void)viewWillAppear:(BOOL)animated
{
[self performSelector:@selector(hideSearchBar) withObject:nil afterDelay:0.0f];
}
- (void)hideSearchBar
{
self.tblView.contentOffset = CGPointMake(0, 40);
}
The problem is it only works for iOS 8.
How can I achieve this correctly to work for both iOS 7 & 8 ????
Sorry for my poor english. Thanks in advance.
Upvotes: 1
Views: 1380
Reputation: 77
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
self.tableView.tableHeaderView = bar;
}
- (void)viewWillAppear:(BOOL)animated
{
[self performSelector:@selector(hideSearchBar) withObject:nil afterDelay:0.0f];
}
- (void)hideSearchBar
{
self.tableView.contentOffset = CGPointMake(0, -24);
}
Upvotes: 1
Reputation: 2190
If you set the header by self.tblView.tableHeaderView = yourSearchBar
(recommended way),try
self.tblView.tableHeaderView = nil;
or
self.tblView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
to hide it.
ps. yourSearchBar
should be a instance variable or a property to be displayed conveniently in the future.
Upvotes: 1