custom UITableView header similar to UISearchBar

2019-02-15 23:35发布

问题:

I want to put my custom header view to UITableView with automatic scroll offset adjustments which UISearchBar have.

How I can do this?

回答1:

Found workaround. I add my header to UISerachBar as subview and hide all other subviews of search bar

-(void)addHeaderView:(UIView *)view
{
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:view.frame];
    [searchBar layoutSubviews];

    UIView *searchBarView = searchBar.subviews[0];

    for (UIView *subview in searchBarView.subviews)
        subview.hidden = YES;

    view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [searchBarView addSubview:view];
    self.tableHeaderView = searchBar;
}