问题
I have a searchBar showing as part of the tableViewHeader as created in the storyboard. However I would like to hide it under some circumstances. To accomplish this, I set the tableHeaderView.frame to a height of 0. To show it again, I set the tableHeaderView.frame to its original height. When it does show it is covered by the first section header(s). Pulling down moves the search bar along with the section header.
I tried [self.searchBar sizeToFit] after it is shown without any success. I also tried [self.view layoutSubviews].
How do I keep the section title from covering the search bar? (iOS 6 and iOS 7)
回答1:
Instead of hiding it by setting its height to zero, you can set the table's content offset to the search bar height. This makes it basically scroll up out of view.
Something like this:
@property (nonatomic, weak) IBOutlet UISearchBar* searchBar;
@property (nonatomic, weak) IBOutlet UITableView* table;
...
- (void) hideSearchBar
{
self.table.contentOffset = CGPointMake( 0, self.searchBar.frame.size.height );
}
来源:https://stackoverflow.com/questions/19010444/uisearchbar-covered-by-section-header