1

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.

user3065901
  • 4,678
  • 11
  • 30
  • 52
  • possible duplicate of [UITableView contentOffSet is not working properly](http://stackoverflow.com/questions/15222186/uitableview-contentoffset-is-not-working-properly) – Yuyutsu May 14 '15 at 08:48
  • I get this solution from that post. The problem is it only works for me in iOS 8. I want to know how to do it for iOS7 too. – user3065901 May 14 '15 at 15:05

2 Answers2

1

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.

liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
  • i dont want to hide all the header. In the header I have more items than the searchbar that i want to show. The searchbar is at the top of all. I only want to display tableview at certain position to only hide the searchbar. – user3065901 May 14 '15 at 15:00
  • @user3065901 May be you shouldn't set it as the headerView of the tableView. Instead, you can add the toolBarView to the tableView's superView as its subView. Then change the frame of the toolBarView in the `scrollViewDidScroll:` method. – liushuaikobe May 15 '15 at 05:45
1
- (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);
}
menextu
  • 77
  • 3