开发者

Showing the searchbar only when user pull down the table

开发者 https://www.devze.com 2023-04-09 11:36 出处:网络
I have a table view wi开发者_开发技巧th a search bar on top of it. My requirement is to do not show the search bar when someone open the page but when someone slides the table down then the search bar

I have a table view wi开发者_开发技巧th a search bar on top of it. My requirement is to do not show the search bar when someone open the page but when someone slides the table down then the search bar should be visible.


In your controller's viewDidAppear: method, set the contentOffset property (in UIScrollView) of your table view to hide the search bar.

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];    
    self.tableView.contentOffset = CGPointMake(0, SEARCH_BAR_HEIGHT);
}


Related to murat's answer, here's a more portable and correct version that will do away with animated offsetting on view load (it assumes the search bar has an outlet property called searchBar):

- (void)viewWillAppear:(BOOL)animated
{
    self.tableView.contentOffset = CGPointMake(0, self.searchBar.frame.size.height);
}

UPDATE:

To accommodate tapping on the search icon in the section index, the following method needs to be implemented, which restores the content offset:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title
               atIndex:(NSInteger)index
{
    index--;
    if (index < 0) {
        [tableView
            setContentOffset:CGPointMake(0.0, -tableView.contentInset.top)];
        return NSNotFound;
    }
    return index;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号