简体   繁体   中英

iOS UITableView content offset not working in iOS7

In my app I have a search bar in the header of my UITableView . 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.

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.

- (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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM