Showing the searchbar only when user pull down the table

前端 未结 2 1370
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-25 08:03

I have a table view with 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 th

相关标签:
2条回答
  • 2020-12-25 08:30

    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 讨论(0)
  • 2020-12-25 08:45

    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);
    }
    
    0 讨论(0)
提交回复
热议问题