UISearchDisplayController table view overlapping search bar

£可爱£侵袭症+ 提交于 2019-12-08 08:10:34

问题


I have a UITableViewController subclass, displayed in a modal view on an iPad. The view controller has a UISearchDisplayController subclass with a UISearchBar included in the header view of the table.

The subclassed UISearchDisplayController is called NoAnimationSearchDisplayController and I have overridden the - (void)setActive:(BOOL)visible animated:(BOOL)animated method to prevent the search bar from animating into the navigation bar when it's set to active. The method override is below...

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{

    if (self.active == visible) {
        return;
    }

    [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];

    [super setActive:visible animated:animated];

    [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];

    if (visible) {
        [self.searchBar becomeFirstResponder];
    } else {
        [self.searchBar resignFirstResponder];
    }

}

The problem I'm having, is that when I have searched, and results are displayed in my search display controller's table view, everything looks fine untill i try to scroll down the list, at this point the content within the cells appears above the search bar, as shown in the following screen:

The search bar is set to UISearchBarStyleMinimal and transparency is enabled. Can anyone let me know how to stop this content overlapping the search bar? Ideally the content would disappear under the search bar as if it was the end of the view.


回答1:


The answer was to manually change the frame of the table view provided by the UIsearchDisplayController in the appropriate delegate method...

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {

    /**
     *  Remove content inset automatically set by UISearchDisplayController as we are forcing the
     *  search bar to stay in the header view of the table, and not go into the navigation bar.
     */

    [tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];


    /**
     *  Recalculate the bounds of our table view to account for the additional 44 pixels taken up by
     *  the search bar, but store this in an iVar to make sure we only adjust the frame once. If we 
     *  don't store it in an iVar it adjusts the frame for every search.
     */

    if (CGRectIsEmpty(_searchTableViewRect)) {

        CGRect tableViewFrame = tableView.frame;

        tableViewFrame.origin.y = tableViewFrame.origin.y + 44;
        tableViewFrame.size.height =  tableViewFrame.size.height - 44;

        _searchTableViewRect = tableViewFrame;

    }

    [tableView setFrame:_searchTableViewRect];

}



回答2:


Be sure the search bar is not translucent enabled ( on storyboard/xib ) or by code. Then make the background to white or whatever color you want.



来源:https://stackoverflow.com/questions/23013401/uisearchdisplaycontroller-table-view-overlapping-search-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!